[英]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
类,它具有公共属性Name
和Connected
。
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" }
}
}
}
};
}
}
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.