我在解决数据模板中的绑定错误时遇到问题。我创建了一个可观察的集合,并为每个文件名设置了 mySelectFlag True。当 Datagrid 显示时,标题上的复选框被选中,并且 datagrid 中的每一行都选中了其复选框。如果我选择/取消选择行复选框,则更改会显示在可观察的集合中。如果我取消选中标题复选框,我会将可观察集合中的每条记录设置为 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>