【发布时间】:2013-11-18 07:44:35
【问题描述】:
我正在尝试将不同 DataGridTemplateColumns 的控件绑定在一起。举个例子:
DataGridTemplateColumn col1 = new DataGridTemplateColumn();
col1.Header = "Source";
FrameworkElementFactory factory1 = new FrameworkElementFactory(typeof(CheckBox));
Binding b1 = new Binding(".");
factory1.SetValue(CheckBox.IsCheckedProperty, b1);
DataTemplate cellTemplate1 = new DataTemplate();
cellTemplate1.VisualTree = factory1;
col1.CellTemplate = cellTemplate1;
dataGrid1.Columns.Add(col1);
DataGridTemplateColumn col2 = new DataGridTemplateColumn();
col2.Header = "Binded to Source";
FrameworkElementFactory factory2 = new FrameworkElementFactory(typeof(TextBox));
Binding b2 = new Binding("What goes here?");
factory2.SetValue(TextBox.IsEnabledProperty, b2); //Enable TextBox if CheckBox of col1 is checked
DataTemplate cellTemplate2 = new DataTemplate();
cellTemplate2.VisualTree = factory2;
col2.CellTemplate = cellTemplate2;
dataGrid1.Columns.Add(col2);
dataGrid1.ItemsSource = new bool[] { true, false };
我想将一个控件的属性(本例中为 CheckBox)绑定到不同 DataGridTemplateColumn 中另一个控件的属性。有可能吗?此绑定是严格的 UI 事物,不会反映在视图模型中。
【问题讨论】:
-
b1 绑定有什么用?
-
b1 将 col1 的 CheckBox 的 IsChecked 属性绑定到模型(本例中为布尔数组)。这工作正常。问题在于 b2 绑定 - 如何绑定到另一列的控件,而不是模型?