WPF DataGrid的更改事件

kognpnkq  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(177)

我对WPF真的很陌生,并试图在数据网格上完成一些事情。我需要获取其中一个单元格MemberId,以触发对数据库的搜索,并使用结果填充其余列。

我尝试使用SelectedCellsChanged和AddingNewItem,但它们似乎在奇怪的时间触发。

<DataGrid Grid.Row="0" x:Name="FirstTimeGrid"
                    AutoGenerateColumns="False"
                    CanUserAddRows="True"
                    CanUserDeleteRows="True"
                    VerticalScrollBarVisibility="Visible"
                    CanUserReorderColumns="False"
                    VerticalAlignment="Top"
                    ItemsSource="{Binding Model.FirstTimeInvoice}"
                    SelectedItem="{Binding Model.FirstTimeInvoiceSelectedItem, Mode=TwoWay}" Grid.ColumnSpan="2"
                    SelectedCellsChanged="FirstTimeGrid_SelectedCellsChanged"
                    AddingNewItem="FirstTimeGrid_AddingNewItem"
                         >
5n0oy7gb

5n0oy7gb1#

你应该处理这件事,也就是说。当数据类中相应列的setter被设置时,执行搜索,例如:

public class Invoice : INotifyPropertyChanged
{
    public Invoice()
    {
        PropertyChanged += OnPropertyChanged;
    }

    ...

    private void OnPropertyChanged(object? sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == nameof(MemberId))
        {
            //TODO: search ...
        }
    }

    private string _memberId;
    public string MemberId
    {
        get { return _memberId; }
        set { _memberId = value; NotifyPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyName = "") =>
         PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

相关问题