WPF数据表格:以编程方式编辑单元格

4

我有一个单元格,需要在单击后设置其值。它与不同的属性绑定。

我应该在哪里做这个?我一直在尝试在 datagrid 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);
1个回答

11

我不确定是否正确理解你的问题,看起来你想以编程方式访问和更改DataGridCell内容。 请查看下面的示例; 我已经添加了SelectedCellsChanged事件处理程序到datagrid,每当选择新单元格时它都应该被触发; 有了DataGridCellInfo对象,您可以访问DataGridCell对象并更改其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;
}

GetVisualChild的代码取自这里

希望它能帮到你,你可能还想看一下SO上关于从代码后台开始编辑特定单元格的问题。我想它也可以给你一些启示。

问候


感谢提供有用的信息。我需要更改的值实际上位于DataGridCell内部的TextBlock中。请参见问题中的单元格模板定义。 - Sharun

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接