简体   繁体   English

如何在DataGrid控件wpf中编辑单元格类型?

[英]How to edit the cell type in a DataGrid Control wpf?

In my application I have a Server class which has public properties Name and Connected . 在我的应用程序中,我有一个Server类,它具有公共属性NameConnected

Connected is a bool which is updated by the each instance of the Server class. Connected是一个bool,由Server类的每个实例更新。

Right now I have DataGrid which is autogenerating columns from a list of servers called servers. 现在我有DataGrid ,它从名为服务器的服务器列表中自动生成列。

Is there a way to change the dataGrid so that it shows "Connected" when Connected is true and "Disconnected" when Connected is false? 有没有办法改变DataGrid的,这样,当显示“已连接” Connected的真实,“断开连接”,当Connected是假的?

if AutoGenerateColumns is used, it is possible to handle AutoGeneratingColumn event in code behind and modify newly created Connected DataGridCheckBoxColumn. 如果使用AutoGenerateColumns ,则可以在代码后面处理AutoGeneratingColumn事件并修改新创建的Connected DataGridCheckBoxColumn。 What I'm going to do, is to create a Trigger which will change status text depending of ChechBox checked state: 我要做的是创建一个触发器,它将根据ChechBox检查状态更改状态文本:

private void DataGridAutoGeneratingColumnHandler(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyName == "Connected")
    {
        var c = e.Column as DataGridCheckBoxColumn;
        if (c == null)
            return;
        c.IsReadOnly = true;
        c.ElementStyle =
            new Style
            {
                TargetType = typeof (CheckBox),
                Setters =
                {
                    new Setter { Property = ContentProperty, Value = "Disconnected" },
                    // prevent checking CheckBoxes 
                    new Setter { Property = IsHitTestVisibleProperty, Value = false },
                },
                Triggers =
                {
                    new Trigger
                    {                                
                        Property = CheckBox.IsCheckedProperty, 
                        Value = true,
                        Setters =
                        {
                            new Setter { Property = ContentProperty, Value = "Connected" }
                        }
                    }
                }
            };
    }
}

结果


another idea: make a special property in a viewModel for connection status description. 另一个想法:在viewModel中创建一个特殊属性,用于连接状态描述。

private void DataGrid_AutoGeneratingColumnHandler(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyName == "Connected")
        e.Cancel = true;
}

and then disable column generation for Connected property 然后禁用Connected属性的列生成

 private void DataGrid_AutoGeneratingColumnHandler(object sender, DataGridAutoGeneratingColumnEventArgs e) { if (e.PropertyName == "Connected") e.Cancel = true; } 

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

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