0

我有一个包含三列的数据网格,它绑定到一个 DataTable。我需要在数据网格的第三列中写一些描述,然后将行的更改写回数据库。只有当我将选择移动到数据网格的下一行时,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;
        }
    }
4

1 回答 1

0

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

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

WPF DataGrid CellEditEnding - 数据集不更新直到行失去焦点

于 2013-10-14T15:50:02.520 回答