DataTable在使用updatesourcetrigger = PropertyChanged绑定到DataGrid时未刷新

时间:2013-10-14 15:00:19

标签: wpf datagrid

我有一个datagrid,它有三列,并绑定到DataTable。 我需要在datagrid的第三列中编写一些描述,然后将行的更改写回数据库。 只有在将选择移动到datagrid的下一行时,DataTable才会刷新。如果选择了一行并且更改了第三项而未移动到下一行,则DataTable源不会立即更改该项。

编辑:应用Geert的解决方案将解决问题或只是将命令按钮放在datagrid所在的相同布局中。

XAML:

<DataGrid AutoGenerateColumns="False"
                          HorizontalAlignment="Stretch"
                          Width="Auto"
                          Height="Auto"
                          CanUserAddRows="False"
                          ItemsSource="{Binding Broadcasters, Mode=TwoWay}">

                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Id"
                                        Width="Auto" IsReadOnly="True" Binding="{Binding Path=SEN_ID}"/>
                            <DataGridTextColumn Header="Sendername"
                                        Width="Auto" IsReadOnly="True" Binding="{Binding Path=SEN_NAME}"/>
                            <DataGridTextColumn Header="Rabattvergabe"
                                        Width="1*" Binding="{Binding Path=Rabatt, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                        </DataGrid.Columns>
     </DataGrid>

ViewModel中的属性:

    /// <summary>
    /// Gets or sets the property value.
    /// </summary>
    public DataTable Broadcasters
    {
        get { return GetValue<DataTable>(BroadcastersProperty); }
        set { SetValue(BroadcastersProperty, value); }
    }

    /// <summary>
    /// Register the Broadcasters property so it is known in the class.
    /// </summary>
    public static readonly PropertyData BroadcastersProperty =
        RegisterProperty("Broadcasters", typeof(DataTable), null);

保存命令:

/// <summary>
    /// Gets the name command.
    /// </summary>
    public Command SaveCmd { get; private set; }

    /// <summary>
    /// Method to invoke when the name command is executed.
    /// </summary>
    private void exec_SaveCmd()
    {
        try
        {
            if (this.Broadcasters != null)
            {
                DataRow[] rows = Broadcasters.Select("Rabatt='1'");
                //Do some work to save the result.
            }
        }
        catch (Exception ex)
        {
            log.Fatal("exec_SaveCmd(): " + ex.Message + ", StackTrace: " + ex.StackTrace);
            throw;
        }
    }

1 个答案:

答案 0 :(得分:0)

实际上这与Catel无关,而是与DataGrid行为有关。

有关更多信息,请参阅此问题(+答案):

WPF DataGrid CellEditEnding - DataSet Not Updating Till Row Lost Focus