gpt4 book ai didi

c# - 使用 observablecollection 绑定(bind)复选框

转载 作者:行者123 更新时间:2023-11-30 23:11:23 26 4
gpt4 key购买 nike

我有一个数据网格,其中有一列我想用数据填充。因此我做了一个模板选择器

<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 中填充了正确的对象和正确的数据。文本框已正确绑定(bind),程序会在失去焦点后通知运行时所做的更改。

现在的问题是复选框。通过初始化,数据网格仅显示正确的数据,我的 observablecollection 没有看到在运行时所做的任何更改(IsChecked 属性)。我需要更新我的收藏。

实现它的最佳方法是什么?我试图实现一个 bool 转换器但没有成功:(谁能帮我 ?

最佳答案

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>

关于c# - 使用 observablecollection 绑定(bind)复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44721761/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com