WPF DataGrid ItemsSource没有更新源更新

时间:2012-06-20 07:26:58

标签: wpf datagrid viewmodel observablecollection itemssource

我已经阅读了一些帖子,但没有人帮我解决问题。 所以,我有一个View with Viewmodel和View a DataGrid boudn到viewmodel里面的ObservableCollection。 Selected Item也绑定到Type 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");
    }

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

你有什么想法吗? :)

1 个答案:

答案 0 :(得分:3)

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