簡體   English   中英

DataGrid-在CellEditEnding()之后觸發的事件

[英]DataGrid - Event which fires after CellEditEnding()

我有一個與對象( ItemsSource )的List <>關聯的DataGrid CellEditEnding ()事件中,我更改了鏈接對象列表的數據。 要刷新DataGrid ,必須刷新它:

this.DataGridFieldProperties.Items.Refresh();

CellEditEnding()事件中的代碼調用更新將引發InvalidOperationException

題:
CellEditEnding()之后是否觸發事件?

到目前為止我嘗試過的
多個事件,例如GotFocusColumnDisplayIndexChanged()等,以及雙向綁定。 但是它們都不可靠地工作,並在異步線程中刷新DataGridTask.Run()異步事件)


<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="40"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Button Name="Btn_FillGrid" Click="Btn_FillGrid_Click"/>
    <DataGrid Name="DataGrid_SOExample" Grid.Row="1" AutoGenerateColumns="False" CanUserAddRows="False" IsReadOnly="False" CellEditEnding="DataGrid_SOExample_CellEditEnding">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Generic1"/>
            <DataGridTextColumn Header="Generic2"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>



public partial class Win_SOExample : Window
{
    public Win_SOExample()
    {
        InitializeComponent();
    }

    private void Btn_FillGrid_Click(object sender, RoutedEventArgs e)
    {
        List<SoExample> soExampList = new List<SoExample>();
        soExampList.Add(new SoExample() { Field1 = "Row0 Field1", Field2 = "Row0 Field2" });
        soExampList.Add(new SoExample() { Field1 = "Row1 Field1", Field2 = "Row1 Field2" });
        soExampList.Add(new SoExample() { Field1 = "Row2 Field1", Field2 = "Row2 Field2" });

        (this.DataGrid_SOExample.Columns[0] as DataGridTextColumn).Binding = new Binding("Field1") { Mode = BindingMode.TwoWay };
        (this.DataGrid_SOExample.Columns[1] as DataGridTextColumn).Binding = new Binding("Field2") { Mode = BindingMode.TwoWay };
        this.DataGrid_SOExample.ItemsSource = soExampList;
    }

    private async void DataGrid_SOExample_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        if(e.EditAction == DataGridEditAction.Commit)
        {
            // after the user finished the edit, data in other rows needs to get updatet

            // simple example
            List<SoExample> soExampList = (this.DataGrid_SOExample.ItemsSource as List<SoExample>);
            soExampList[1].Field1 = DateTime.Now.ToLongDateString();

            await Task.Yield();
            this.DataGrid_SOExample.Items.Refresh();
        }
    }

    private class SoExample
    {
        public string Field1 { get; set; } = "";
        public string Field2 { get; set; } = "";
    }
}

您應該在您的SoExample類中實現INotifyPropertyChanged

private class SoExample : INotifyPropertyChanged
{
    private string _field1;
    public string Field1
    {
        get { return _field1; }
        set { _field1 = value; NotifyPropertyChanged(); }
    }

    private string _field2;
    public string Field2
    {
        get { return _field2; }
        set { _field2 = value; NotifyPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

然后,您可以將屬性設置為新值而無需刷新:

private async void DataGrid_SOExample_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    if (e.EditAction == DataGridEditAction.Commit)
    {
        List<SoExample> soExampList = (this.DataGrid_SOExample.ItemsSource as List<SoExample>);
        soExampList[1].Field1 = DateTime.Now.ToLongDateString();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM