【发布时间】:2009-11-19 16:16:23
【问题描述】:
我有一个单元格,它需要在刚刚被点击时设置它的值。它与不同的属性多重绑定。
我应该在哪里执行此操作?我一直在尝试在这样的数据网格开始编辑处理程序中执行此操作(没有太大成功)。我可以手动单击两次(一次选择单元格然后开始编辑)并设置值。但我想以编程方式执行此操作...
private void MyDataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
TextBlock t = e.EditingEventArgs.OriginalSource as TextBlock;
if (t == null) return;
t.Text = SimulatedEdit();
// All this below is just me trying different thing. Not sure what I need to be doing
e.EditingEventArgs.Handled = true;
MyDataGrid.CommitEdit();
MyDataGrid.UnselectAllCells();
}
这就是列模板的设置方式
MultiBinding tempmb = new MultiBinding();
Binding tempXB = new Binding("X");
Binding temptYB = new Binding("Y");
tempmb.Bindings.Add(tempXB);
tempmb.Bindings.Add(temptYB);
tempmb.ConverterParameter = "ggrid";
tempmb.Converter = new LabelDecider();
DataGridTemplateColumn dgtc = new DataGridTemplateColumn
{
Header = "blah", CanUserSort = false, CanUserReorder = false,
};
DataTemplate t = new DataTemplate();
FrameworkElementFactory f = new FrameworkElementFactory(typeof(TextBlock));
f.SetBinding(TextBlock.TextProperty, tempmb);
// Setup background color binding
MultiBinding colorb = new MultiBinding();
colorb.Bindings.Add(tempX);
colorb.Bindings.Add(tempY);
colorb.ConverterParameter = "color";
colorb.Converter = new LabelDecider();
f.SetBinding(TextBlock.BackgroundProperty, colorb);
t.VisualTree = f;
//Every columns Text and Background are using bindings
dgtc.CellTemplate = t;
//setup editing template
DataTemplate ced = new DataTemplate();
FrameworkElementFactory f2 = new FrameworkElementFactory(typeof(TextBox));
MultiBinding tempmb2 = new MultiBinding();
tempmb2.Bindings.Add(tempXB);
tempmb2.Bindings.Add(tempYB);
tempmb2.Mode = BindingMode.TwoWay;
tempmb2.ConverterParameter = "ggrid";
tempmb2.Converter = new LabelDecider(rDestination.Recievers[k]);
tempmb2.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
f2.SetBinding(TextBox.TextProperty, tempmb2);
ced.VisualTree = f2;
dgtc.CellEditingTemplate = ced;
MyDataGrid.Columns.Add(dgtc);
【问题讨论】: