我有一个带有列的数据网格,我想填充数据。 因此我做了一个模板选择器
<DataGridTemplateColumn Header="Value" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplateSelector>
<c:checkBoxCheckConverter
ValueIsBoolean="{StaticResource ValueIsBoolean}"
ValueIsColumn3="{StaticResource ValueIsColumn3}"/>
</DataGridTemplateColumn.CellTemplateSelector>
</DataGridTemplateColumn>
</DataGrid.Columns>
模板:
public class checkBoxCheckConverter : DataTemplateSelector
{
public DataTemplate ValueIsBoolean
{ get; set; }
public DataTemplate ValueIsColumn3
{ get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
cDGStructure cdgStructure = item as cDGStructure;
if (cdgStructure != null)
{
if (((cdgStructure.Column3) == "True") | ((cdgStructure.Column3) == "False"))
return ValueIsBoolean;
else
// return ValueIsNotBoolean;
cdgStructure = cdgStructure;
return ValueIsColumn3;
}
else
return base.SelectTemplate(item, container);
}
}
<DataTemplate x:Key="ValueIsBoolean">
<CheckBox Content="">
<CheckBox.Style>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="IsChecked" Value="False"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Column3, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="True">
<Setter Property="IsChecked" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
</DataTemplate>
<DataTemplate x:Key="ValueIsColumn3">
<TextBox Text="{Binding Column3, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/>
</DataTemplate>
使用的数据结构:
public class cDGStructure : INotifyPropertyChanged
{
private string _column1; //name
private string _column2; //type
private string _column3; //value
private string _column4; //comment
//comment
public cDGStructure(string column1, string column2, string column3, string column4)
{
_column1 = column1;
_column2 = column2;
_column3 = column3;
_column4 = column4;
}
public string Column1
{
get { return _column1; }
set
{
_column1 = value;
NotifyPropertyChanged("Column1");
}
}
public string Column2
{
get { return _column2; }
set
{
_column2 = value;
NotifyPropertyChanged("Column2");
}
}
public string Column3
{
get { return _column3; }
set
{
_column3 = value;
NotifyPropertyChanged("Column3");
}
}
public string Column4
{
get { return _column4; }
set
{
_column4 = value;
NotifyPropertyChanged("Column4");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region Private Helpers
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
网格填充如下:
public ObservableCollection<cDGStructure> collSymbolsDataGrid = new ObservableCollection<cDGStructure>();
moi = "moi";
waar = "True";
nwaar = "False";
collSymbolsDataGrid.Add(new cDGStructure(moi, moi, nwaar, moi));
collSymbolsDataGrid.Add(new cDGStructure(moi, moi, waar, moi));
collSymbolsDataGrid.Add(new cDGStructure(moi, moi, moi, moi));
InitializeComponent();
dgSymbolGrid.ItemsSource = collSymbolsDataGrid;
Datagrid中填充了正确的对象和正确的数据。 文本框已正确绑定,程序会在失去焦点后通知运行时所做的更改。
现在问题是复选框。通过初始化,datagrid显示正确的数据,只有我的observablecollection在运行时没有看到任何更改(IsChecked属性)。我需要更新我的收藏品。
实现此目的的最佳方法是什么? 我试图实现一个布尔转换器,但没有成功:( 谁能帮我 ?
答案 0 :(得分:2)
TemplateSelector不适合更改动态更改属性值的对象。它用于选择对象类型或不变属性值等内容。
对于属性的动态更改,请改用样式触发器。类似于以下内容:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellStyle>
<Style TargetType="DataGridCell">
<!-- Template or ContentTemplate? -->
<Setter Property="Template" Value="{StaticResource ValueIsColumn3}"/>
<Style.Triggers>
<!-- Alternative: use an IsBoolean converter instead of testing True and False separately -->
<DataTrigger Binding="{Binding Column3}" Value="True">
<!-- Template or ContentTemplate? -->
<Setter Property="Template" Value="{StaticResource ValueIsBoolean}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Column3}" Value="False">
<!-- Template or ContentTemplate? -->
<Setter Property="Template" Value="{StaticResource ValueIsBoolean}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>