WPF的Datagrid:以编程方式编辑单元格 [英] WPF Datagrid: Programmatically editing a cell
问题描述
我有一个需要它的值的单元格上,它只是被点击进行设置。它multibound到不同的性质。
我应该在哪里做呢?我一直在试图做这样在数据网格beginingedit处理器(没有成功)。我可以手动点击两次(一次选择单元格,然后开始编辑)与价值获取设置。但我想以编程方式做到这一点...
私人无效MyDataGrid_BeginningEdit(对象发件人,DataGridBeginningEditEventArgs E)
$ { b
$ b TextBlock的T = e.EditingEventArgs.OriginalSource作为TextBlock的;
如果(T == NULL)回报;
t.Text = SimulatedEdit();
//下面这一切都只是我尝试不同的事情。不知道我需要做
什么e.EditingEventArgs.Handled = TRUE;
MyDataGrid.CommitEdit();
MyDataGrid.UnselectAllCells();
}
这是columntemplate是如何设置
MultiBinding tempmb =新MultiBinding();
结合tempXB =新绑定(X);
结合temptYB =新的绑定(Y);
tempmb.Bindings.Add(tempXB);
tempmb.Bindings.Add(temptYB);
tempmb.ConverterParameter =ggrid;
tempmb.Converter =新LabelDecider();
DataGridTemplateColumn DGTC =新DataGridTemplateColumn
{
头=嗒嗒,CanUserSort =假,CanUserReorder =假,
};
的DataTemplate T =新的DataTemplate();
FrameworkElementFactory F =新FrameworkElementFactory(typeof运算(TextBlock中));
f.SetBinding(TextBlock.TextProperty,tempmb);
//设置背景颜色结合
MultiBinding colorb =新MultiBinding();
colorb.Bindings.Add(tempX);
colorb.Bindings.Add(tempY);
colorb.ConverterParameter =颜色;
colorb.Converter =新LabelDecider();
f.SetBinding(TextBlock.BackgroundProperty,colorb);
t.VisualTree = F;
//每列文本和背景都使用绑定
dgtc.CellTemplate = T;
//设置编辑模板
的DataTemplate土木工程署=新的DataTemplate();
FrameworkElementFactory F2 =新FrameworkElementFactory(typeof运算(文本框));
MultiBinding tempmb2 =新MultiBinding();
tempmb2.Bindings.Add(tempXB);
tempmb2.Bindings.Add(tempYB);
tempmb2.Mode = BindingMode.TwoWay;
tempmb2.ConverterParameter =ggrid;
tempmb2.Converter =新LabelDecider(rDestination.Recievers [K]);
tempmb2.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
f2.SetBinding(TextBox.TextProperty,tempmb2);
ced.VisualTree = F2;
dgtc.CellEditingTemplate = CED;
MyDataGrid.Columns.Add(DGTC);
不知道如果我正确理解你的问题;它看起来像你想访问和编程方式更改DataGridCell内容。请检查下面的例子;我添加了一个SelectedCellsChanged甚至弯到DataGrid,应该引起每一次新的单元格被选中;具有DataGridCellInfo对象,你可以访问DataGridCell对象并更改其内容。
私人无效dataGrid1_SelectedCellsChanged(对象发件人,SelectedCellsChangedEventArgsê )
{
的foreach(DataGridCellInfo cellInfo在dataGrid1.SelectedCells)
{
//这改变了单元格内容不是其背后的数据项
DataGridCell栅格单元= TryToFindGridCell( DataGrid1中,cellInfo);
如果(栅格单元!= NULL)gridCell.Content =改变!!!;
}
}
静态DataGridCell TryToFindGridCell(DataGrid中网格DataGridCellInfo cellInfo)
{
DataGridCell结果= NULL;
DataGridRow行=(DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(cellInfo.Item);
如果(!行= NULL)
{
INT参数:columnIndex = grid.Columns.IndexOf(cellInfo.Column);
如果(参数:columnIndex -1个)
{
DataGridCellsPresenter演示= GetVisualChild&所述; DataGridCellsPresenter>(行);
结果= presenter.ItemContainerGenerator.ContainerFromIndex(参数:columnIndex)为DataGridCell;
}
}
返回结果;
}
静态牛逼GetVisualChild< T>(视觉父),T:可视
{$ B $(B T)=孩子默认(T);
INT numVisuals = VisualTreeHelper.GetChildrenCount(父);
的for(int i = 0; I< numVisuals;我++)
{
视觉V =(视频)VisualTreeHelper.GetChild(父母,我);
子= V为T;
如果(孩子== NULL)
{
=子&GetVisualChild LT; T>(五);
}
如果
{
中断(子!= NULL);
}
}
返回子女;
}
GetVisualChild的代码是从的这里
希望这会帮助你,你也maight要看看从代码中的特定细胞的 BeginEdit背后SO 问题。我想这也可以给你一些想法
关于
I have a cell that needs its value to be set on it just being clicked. It is multibound to different properties.
Where am I supposed to do this? I have been trying to do it in the datagrid beginingedit handler like this (without much success). I am able to manually click twice(once to select cell and then to start edit) and the value gets set. But I want to do this programmatically...
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();
}
This is how the columntemplate is setup
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);
not sure if I'm understanding your question correctly; it looks like you want to access and change the DataGridCell content programmatically. Pls check an example below; I've added a SelectedCellsChanged even hander to the datagrid, it should be triggered every time new cell(s) is selected; having the DataGridCellInfo object you can get access to the DataGridCell object and change its Content.
private void dataGrid1_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
foreach (DataGridCellInfo cellInfo in dataGrid1.SelectedCells)
{
// this changes the cell's content not the data item behind it
DataGridCell gridCell = TryToFindGridCell(dataGrid1, cellInfo);
if (gridCell!=null) gridCell.Content = "changed!!!";
}
}
static DataGridCell TryToFindGridCell(DataGrid grid, DataGridCellInfo cellInfo)
{
DataGridCell result = null;
DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(cellInfo.Item);
if (row!=null)
{
int columnIndex = grid.Columns.IndexOf(cellInfo.Column);
if (columnIndex>-1)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
result = presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex) as DataGridCell;
}
}
return result;
}
static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
code of GetVisualChild is taken from here
hope it's going to help you, also you maight want to take a look at the BeginEdit of a specific Cell from code behind question on SO. I guess it can also give you some ideas
regards
这篇关于WPF的Datagrid:以编程方式编辑单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!