WPF DataGrid ItemsSource 在更改源时不会更新

标签 wpf datagrid viewmodel observablecollection itemssource

我已经阅读了一些帖子,但没有人帮助我解决我的问题。 所以,我有一个带有 Viewmodel 的 View ,并且在 View 内部有一个 DataGrid 绑定(bind)到 View 模型内的 ObservableCollection。 所选项目也绑定(bind)到类型 T == ObservableCollection

public ObservableCollection<TableProperty> TableProperties
    {
        get
        {
            return tableProperties;
        }
        set
        {
            if (tableProperties != value)
            {
                tableProperties = value;
                OnPropertyChanged("TableProperties");
            }
        }
    }

public TableProperty Property
    {
        get
        {
            return property;
        }
        set
        {
            property = value;
            OnPropertyChanged("Property");
        }
    }

这里是 DataGrid:

<toolkit:DataGrid AutoGenerateColumns="False" 
                      UseLayoutRounding="True" 
                      ItemsSource="{Binding Path=TableProperties,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,NotifyOnTargetUpdated=True}"
                      SelectedItem="{Binding Path=Property,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                      [...]>
        <toolkit:DataGrid.Columns>

现在我想实现一个逻辑,更改复选框会触发设置所选项目的某些值的命令:

<toolkit:DataGridTemplateColumn Header="Mandatory" IsReadOnly="False">
                <toolkit:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                            <CheckBox IsChecked="{Binding Path=Mandatory,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
                                <CheckBox.Style>
                                    <Style TargetType="{x:Type CheckBox}">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Path=MandatoryDB}" Value="True">
                                                <Setter Property="IsEnabled" Value="False" />
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding Path=MandatoryDB}" Value="False">
                                                <Setter Property="IsEnabled" Value="True" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </CheckBox.Style>
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Checked">
                                        <cmd:EventToCommand Command="{Binding Path=DataContext.SetMandatory, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </CheckBox>
                        </StackPanel>
                    </DataTemplate>
                </toolkit:DataGridTemplateColumn.CellTemplate>
            </toolkit:DataGridTemplateColumn>

这是它背后的命令:

 private void OnSetMandatory()
    {
        property.Visible = true;
        property.ReadOnly = false;
        property.VisibleInGrid = true;
        property.UIPropertyName = DateTime.Now.TimeOfDay.ToString();
        OnPropertyChanged("Property");
        OnPropertyChanged("TableProperties");
    }

问题是:当我更改属性时,集合内的项目也已更新,并且它在属性 Getter 内正常运行... 如果我直接在 View 内调用 Datagrid.Items.Refresh(),它也会正确显示值,但不会自动更新集合。

那么你有什么想法吗? :)

最佳答案

TableProperty 类必须实现 INotifyPropertyChanged 并正确引发它。

关于WPF DataGrid ItemsSource 在更改源时不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11114759/

相关文章:

带有验证错误样式的 WPF 数据网格单元

c# - AutoMapper - 如何使用 ConstructedBy 方法将参数传递给自定义解析器?

wpf - "IsAsync"对慢属性没有影响?

c# - 中继命令没有被触发

wpf - 从 MahApps FlipView 中删除边框

c# - 在 WPF 中对数据绑定(bind)进行单元测试

c# - 如何: Read the content of a selected DataGrid row

c# - 在 C# WPF 中从 MySQL 数据库填充 dataGrid

c# - 将 DataTemplate 中的 WPF RelayCommand 绑定(bind)到 UserControl 内的按钮

c# - 获取对 View 模型当前实例的引用