使用 DataTemplate 将 OnClick 附加到布尔更改

发布于 2024-08-11 12:55:23 字数 1125 浏览 4 评论 0原文

我想创建一个数据模板(在代码中,但这不是重点),它允许我单击一个项目并设置其布尔值。我设法创建的是 CheckBox 和 TextBlock 的组合,它的颜色取决于 bool 值。

到目前为止一切顺利...但是我如何告诉 WPF:如果有人单击 TextBlock,请更改 bool 值。(消除对丑陋复选框的需要)

到目前为止代码并正常工作:

var dT = new DataTemplate(typeof(DirectoryWrapper));
var stackPanel = new FrameworkElementFactory(typeof(StackPanel));

var style = new Style(typeof(TextBlock));
var t = new DataTrigger() {Binding = new Binding(DirectoryWrapper.PropString(x => x.IsSelected)), Value = true};
t.Setters.Add(new Setter() { Property = TextBox.ForegroundProperty, Value = new SolidColorBrush(Colors.Green) });
style.Triggers.Add(t);


var box = new FrameworkElementFactory(typeof(CheckBox));
box.SetBinding(CheckBox.IsCheckedProperty, new Binding(DirectoryWrapper.PropString(x => x.IsSelected)));
stackPanel.AppendChild(box);

var entry = new FrameworkElementFactory(typeof(TextBlock));
entry.SetBinding(TextBlock.TextProperty, new Binding(DirectoryWrapper.PropString(x => x.Path)));
entry.SetValue(TextBox.StyleProperty, style);
stackPanel.AppendChild(entry);

dT.VisualTree = stackPanel;
return dT;

I want to create a datatemplate (in code, but thats not the point) which allows me to click on an item and set its bool value. What I managed to create was a combination of CheckBox and TextBlock, which is colored depending on the bool value.

So far so good... But how can I tell WPF: If anybody clicks on the TextBlock, change the bool value. (removing the need for the ugly checkbox)

Code so far and working:

var dT = new DataTemplate(typeof(DirectoryWrapper));
var stackPanel = new FrameworkElementFactory(typeof(StackPanel));

var style = new Style(typeof(TextBlock));
var t = new DataTrigger() {Binding = new Binding(DirectoryWrapper.PropString(x => x.IsSelected)), Value = true};
t.Setters.Add(new Setter() { Property = TextBox.ForegroundProperty, Value = new SolidColorBrush(Colors.Green) });
style.Triggers.Add(t);


var box = new FrameworkElementFactory(typeof(CheckBox));
box.SetBinding(CheckBox.IsCheckedProperty, new Binding(DirectoryWrapper.PropString(x => x.IsSelected)));
stackPanel.AppendChild(box);

var entry = new FrameworkElementFactory(typeof(TextBlock));
entry.SetBinding(TextBlock.TextProperty, new Binding(DirectoryWrapper.PropString(x => x.Path)));
entry.SetValue(TextBox.StyleProperty, style);
stackPanel.AppendChild(entry);

dT.VisualTree = stackPanel;
return dT;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小鸟爱天空丶 2024-08-18 12:55:23

这在 WPF 中是微不足道的:只需将您的 CheckBox 模板化为看起来像 TextBlock:

<CheckBox>
  <CheckBox.Template>
    <ControlTemplate>
      <TextBlock Binding="{Binding WhateverYouWant}" ... />
    </ControlTemplate>
  </CheckBox.Template>
</CheckBox>

这可以通过在 TextBlock 周围添加 Border 或任何您喜欢的其他内容来扩展披萨。

您想要使用 CheckBox 而不是 ToggleButton 的原因是 CheckBox 有额外的键盘支持,以及可映射到复选框范例的可访问性支持辅助功能设备。 ToggleButton 不提供这些功能。

This is trivial in WPF: Just template your CheckBox to look like a TextBlock:

<CheckBox>
  <CheckBox.Template>
    <ControlTemplate>
      <TextBlock Binding="{Binding WhateverYouWant}" ... />
    </ControlTemplate>
  </CheckBox.Template>
</CheckBox>

This might be extended by adding a Border around the TextBlock or anything else you like to give it more pizzaz.

The reason you want to use CheckBox instead of ToggleButton is that CheckBox has additional keyboard suport, plus accessibilty support to map into the checkbox paradigm on the accessibilty device. ToggleButton doesn't give you these features.

甜嗑 2024-08-18 12:55:23

您不能使用切换按钮并创建您需要的任何样式/控制模板来获得您想要的外观吗? ToggleButton 将根据您的点击将其 IsChecked 属性设置为 true 或 false。因此,将您的 Data 属性绑定到 ToggleButton.IsSelected

Cant you use a Toggle button and create whatever style/Control template you need to get your desired look. A ToggleButton will set its IsChecked property to true or false based on your click. So bind your Data property to ToggleButton.IsSelected

凉月流沐 2024-08-18 12:55:23

好的,
只是为了方便起见,Ray Burns 解决方案的工作代码:(

对于初学者:PropString 函数只是一个删除魔术字符串的包装器,使用您在此处绑定的属性名称字符串...)

var dT = new DataTemplate(typeof (DirectoryWrapper));

// Create style to set text red if checked
var style = new Style(typeof (TextBlock));
var t = new DataTrigger() {Binding = new Binding(PropString(x => x.IsSelected)), Value = true};
t.Setters.Add(new Setter() {Property = Control.ForegroundProperty, Value = new SolidColorBrush(Colors.Red)});
style.Triggers.Add(t);

// Create text box
var entry = new FrameworkElementFactory(typeof(TextBlock));
entry.SetBinding(TextBlock.TextProperty, new Binding(PropString(x => x.Path)));
entry.SetValue(FrameworkElement.StyleProperty, style);

// Put into template
var boxTemplate= new ControlTemplate(typeof(CheckBox)) {VisualTree = entry};

// Create box and set template
var box = new FrameworkElementFactory(typeof (CheckBox));
box.SetBinding(ToggleButton.IsCheckedProperty, new Binding(PropString(x => x.IsSelected)));
box.SetValue(Control.TemplateProperty, boxTemplate);

dT.VisualTree = box;
return dT;

Ok,
just for convience, the working code for Ray Burns Solution:

(For beginners: The PropString function is just a wrapper to remove magic strings, use string of property name you bind to here...)

var dT = new DataTemplate(typeof (DirectoryWrapper));

// Create style to set text red if checked
var style = new Style(typeof (TextBlock));
var t = new DataTrigger() {Binding = new Binding(PropString(x => x.IsSelected)), Value = true};
t.Setters.Add(new Setter() {Property = Control.ForegroundProperty, Value = new SolidColorBrush(Colors.Red)});
style.Triggers.Add(t);

// Create text box
var entry = new FrameworkElementFactory(typeof(TextBlock));
entry.SetBinding(TextBlock.TextProperty, new Binding(PropString(x => x.Path)));
entry.SetValue(FrameworkElement.StyleProperty, style);

// Put into template
var boxTemplate= new ControlTemplate(typeof(CheckBox)) {VisualTree = entry};

// Create box and set template
var box = new FrameworkElementFactory(typeof (CheckBox));
box.SetBinding(ToggleButton.IsCheckedProperty, new Binding(PropString(x => x.IsSelected)));
box.SetValue(Control.TemplateProperty, boxTemplate);

dT.VisualTree = box;
return dT;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文