English 中文(简体)
Using a DataTemplate to attach OnClick to bool change
原标题:

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;
最佳回答

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.

问题回答

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

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;




相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签