【问题标题】:DataTable did not refresh when it is bound to DataGrid using updatesourcetrigger=PropertyChangedDataTable 使用 updatesourcetrigger=PropertyChanged 绑定到 DataGrid 时没有刷新
【发布时间】:2013-10-22 04:39:08
【问题描述】:

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

编辑:应用 Geert 的解决方案将解决问题,或者简单地将命令按钮放在数据网格所在的相同布局。

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;
        }
    }

【问题讨论】:

    标签: wpf datagrid


    【解决方案1】:

    其实这与 Catel 无关,而是与 DataGrid 的行为有关。

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

    WPF DataGrid CellEditEnding - DataSet Not Updating Till Row Lost Focus

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 1970-01-01
      • 2019-09-10
      • 2010-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多