我在解决数据模板中的绑定错误时遇到问题。我创建了一个可观察的集合,并为每个文件名设置mySelectFlag True。显示Datagrid时,将检查标题上的复选框,并且datagrid中的每一行都选中其复选框。如果我选中/取消选中行复选框,则更改会显示在可观察集合上。如果取消选中标题复选框,我会将observable集合上的每条记录设置为false。我在可观察集合上调用OnPropertyChanged,但数据网格不反映更改。没有绑定错误。谁能告诉我我做错了什么? 非常感谢你。
我有一个班级:
public class InputFileName
{
public bool mySelectFlag { get; set; }
public string myFileName { get; set; }
}
和一个ObservableCollection:
private ObservableCollection<InputFileName> _DisplayList;
public ObservableCollection<InputFileName> DisplayList
{
get { return _DisplayList; }
set
{
if (value != _DisplayList)
{
_DisplayList = value;
OnPropertyChanged("DisplayList");
}
}
}
这是我的xaml:
<DataGrid
Margin="25"
RowHeaderWidth="0"
AutoGenerateColumns="False"
AlternatingRowBackground="Gainsboro"
AlternationCount="2"
Block.TextAlignment="Center"
CanUserAddRows="False"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserSortColumns="False"
ItemsSource="{Binding Path=DisplayList}" >
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<CheckBox
IsChecked="{Binding Path=DataContext.IsSelected, ElementName=myControl1, FallbackValue=False}"
Command="{Binding Path=DataContext.SelectAllRows, ElementName=myControl1}"
Content="Select"
FontWeight="Bold"
Width="Auto" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGridTemplateColumn.HeaderStyle>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox
HorizontalAlignment="Center"
VerticalAlignment="Center"
IsChecked="{Binding Path=mySelectFlag, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, FallbackValue=False}"
Command="{Binding Path=DataContext.TestTaskCommand, ElementName=myControl1}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=SelectedIndex}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn
Header="File Name"
FontWeight="Bold"
Width="Auto"
Binding="{Binding Path=myFileName}" />
</DataGrid.Columns>
</DataGrid>
答案 0 :(得分:2)
您需要在INotifyPropertyChanged
课程上实施InputFileName
界面,以便对UI进行更改 -
private bool mySelectFlag;
public bool MySelectFlag
{
get
{
return mySelectFlag;
}
set
{
mySelectFlag = value;
OnPropertyChanged("MySelectFlag");
}
}
答案 1 :(得分:0)
INotifyPropertyChanged接口由你的类实现,一个方法应该定义如下,所有属性都应该调用OnPropertyChangeEvent
公共事件PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string e)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(e));
}