gpt4 book ai didi

c# - WPF 数据网格 : Programmatically editing a cell

转载 作者:行者123 更新时间:2023-11-30 14:04:32 27 4
gpt4 key购买 nike

我有一个单元格需要在单击时设置其值。它与不同的属性是多重绑定(bind)的。

我应该在哪里做这个?我一直在像这样在数据网格 beginingedit 处理程序中尝试这样做(没有太大成功)。我可以手动单击两次(一次选择单元格然后开始编辑)并设置值。但我想以编程方式执行此操作...

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);

最佳答案

不确定我是否正确理解了您的问题;看起来您想以编程方式访问和更改 DataGridCell 内容。请检查下面的示例;我已经向数据网格添加了一个 SelectedCellsChanged 甚至处理程序,它应该在每次选择新单元格时触发;有了 DataGridCellInfo 对象,您就可以访问 DataGridCell 对象并更改其内容。

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;
}

GetVisualChild 代码取自here

希望它能帮到你,你也可能想看看 BeginEdit of a specific Cell from code behind关于SO的问题。我想它也可以给你一些想法

问候

关于c# - WPF 数据网格 : Programmatically editing a cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1764498/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com