2009-11-16 10 views
1

我想创建一个datatemplate(在代码中,但那不是重点),它允许我单击一个项目并设置它的布尔值。我设法创建的是CheckBox和TextBlock的组合,它根据bool值着色。使用DataTemplate将OnClick附加到布尔变化

到目前为止这么好...但我怎么能告诉WPF:如果有人点击TextBlock,更改布尔值。(消除对丑陋的复选框的需要)到目前为止

代码和工作:

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; 

回答

2

这是微不足道的WPF:只要你的模板复选框,看起来像一个TextBlock:

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

这可以通过增加一个Border围绕TextBlock或其他任何扩展你想给它更多的比萨饼。

要使用的CheckBox代替ToggleButton的原因是CheckBox有额外的键盘询问服务,加上平易支持映射到平易设备上的复选框范例。 ToggleButton不会为您提供这些功能。

+0

这是时刻,当我真的很喜欢WPF。像魅力一样工作。 Postet代码为您的解决方案在下面单独发布(对于其他可能需要代码示例代替XAML) – 2009-11-17 12:22:44

+0

+1工作解决方案!我正在寻找一种解决方法,但这种方法就像魅力一样! – 2010-10-26 07:44:22

0

广东话您使用切换按钮,然后创建任何风格/控制模板,你需要得到你想要的样子。根据您的点击,ToggleButton会将其IsChecked属性设置为true或false。所以,你的数据属性绑定到ToggleButton.IsSelected

0

好吧, 只是为了便于学习,雷伯恩斯解决方案的工作代码:

(对于初学者:该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; 
+0

看起来非常好。仅供参考,您会惊讶地发现,在这种情况下,在代码中创建对象的效率低于XAML。在编译期间,您的XAML将转换为一种称为“BAML”的非常有效的格式。这种格式实际上比FrameworkElementFactory更有效。由于FrameworkElementFactory无法利用某些内部基于BAML的优化,因此它还节省了大量内存。但是出于实际的目的,这种性能差异不太可能是重要的,因此代码与XAML的选择应该基于其他因素。 – 2009-11-18 04:01:31

相关问题