views:

87

answers:

3

Hi,

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;
A: 

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

Jobi Joy
+2  A: 

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.

Ray Burns
That are moments when i really like WPF. Worked like a charm. Postet code for your solution below in seperate post (for others who might need code samples instead of XAML)
Christian
+1 for working solution! I was looking for a workaround solution, but this works like a charm!
Peter van Kekem
A: 

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;
Christian
Looks very good. FYI, you'll be surprised to learn that in this case creating the object in code is less efficient in than XAML. During compilation your XAML is converted to an very efficient format called "BAML." This format is actually more efficient than FrameworkElementFactory for instantiating templates. It also saves a lot of RAM because of some internal BAML-based optimizations that FrameworkElementFactory can't take advantage of. But for practical purposes this performance difference is not likely to matter much so the choice of code vs XAML should be based on other factors.
Ray Burns