简体   繁体   English

WPF Datagrid:以编程方式编辑单元格

[英]WPF Datagrid: Programmatically editing a cell

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). 我一直在尝试像这样在datagrid Beginningedit处理程序中执行此操作(没有很大的成功)。 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 这是设置columntemplate的方式

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. 看起来您想以编程方式访问和更改DataGridCell内容。 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; 我已经向数据网格添加了SelectedCellsChanged甚至处理程序,它应该在每次选择新单元格时触发; having the DataGridCellInfo object you can get access to the DataGridCell object and change its Content. 具有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;
}

code of GetVisualChild is taken from here GetVisualChild的代码是从这里获取的

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. 希望它能对您有所帮助,您也可能希望 SO 背后的代码中了解特定CellBeginEdit I guess it can also give you some ideas 我想它也可以给你一些想法

regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM