UWP DataGridComboBoxColumn 在选择更改时更改行值

UWP DataGridComboBoxColumn change row values when selection changes

我正在做一个项目,我需要 select 对象列表中的一个值,然后根据 selection 更新同一行中显示的其他值制作,具有 selected 对象的属性。我将我的 VM 中的列表(使用 Prism)绑定到 DataGridComboBoxColumn 并且工作正常,但是没有我可以绑定到的事件 SelectionChanged。我实际上使用的是 Telerik 的 RadDataGrid,我的代码是:

<telerikGrid:RadDataGrid x:Name="ArticlesDataGrid" Margin="0,8,0,0"
                                 ItemsSource="{x:Bind MasterMenuItem.Articles, Mode=TwoWay}" UserEditMode="Inline" UserGroupMode="Disabled" AutoGenerateColumns="False">
        <telerikGrid:RadDataGrid.Columns>
            <telerikGrid:DataGridComboBoxColumn PropertyName="Code" ItemsSource="{x:Bind ViewModel.Articles}" DisplayMemberPath="Code" SelectedValuePath="Code" />
            <telerikGrid:DataGridComboBoxColumn PropertyName="Description" ItemsSource="{x:Bind ViewModel.Articles}" DisplayMemberPath="Description" SelectedValuePath="Description" />
            <telerikGrid:DataGridTextColumn PropertyName="MeasureUnit.Description" CanUserEdit="False" />
            <telerikGrid:DataGridTextColumn PropertyName="VatCode.Description" CanUserEdit="False"/>
            <telerikGrid:DataGridTextColumn PropertyName="Price" CanUserEdit="False"/>
            <telerikGrid:DataGridTextColumn PropertyName="Quantity" />
            <telerikGrid:DataGridTextColumn PropertyName="Total" CanUserEdit="False" />
        </telerikGrid:RadDataGrid.Columns>
    </telerikGrid:RadDataGrid>

我需要当Description 或Code 列发生变化时,我可以更新同一行其他列中的内容。 有人能指出我正确的方向吗?

谢谢!

您不需要 SelectionChanged 事件。您只需要让您的模型 class 实现 INotifyPropertyChanged Interface,当 DataGridComboBoxColumn 的值改变时,模型对象的 属性 值也会改变。然后,您可以更改其中其他属性的值。

由于您没有提供您的代码隐藏代码,所以我只做一个简单的代码示例供您参考:

<telerikGrid:RadDataGrid x:Name="ArticlesDataGrid" Margin="0,8,0,0"
                             ItemsSource="{x:Bind ViewModel}" UserEditMode="Inline" UserGroupMode="Disabled" AutoGenerateColumns="False" >
        <telerikGrid:RadDataGrid.Columns>
            <telerikGrid:DataGridComboBoxColumn PropertyName="Option" ItemsSourcePath="Options" />
            <telerikGrid:DataGridTextColumn PropertyName="Total" CanUserEdit="False" />
        </telerikGrid:RadDataGrid.Columns>
    </telerikGrid:RadDataGrid>
public class Test : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string PropertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }
    }

    private int _Total;
    public int Total
    {
        get { return _Total; }
        set
        {
            if (_Total != value)
            {
                _Total = value;
                RaisePropertyChanged("Total");
            }
        }
    }

    private string _Option;
    public string Option
    {
        get { return _Option; }
        set
        {
            if (_Option != value)
            {
                _Option = value;
                switch (value)
                {
                    case "option1":this.Total = 1;break;
                    case "option2": this.Total = 2;break;
                    default: this.Total = 0;break;
                }
                RaisePropertyChanged("Option");
            }
        }
    }

    public ObservableCollection<string> Options { get; set;}
}
public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        ViewModel = new ObservableCollection<Test>();
        ViewModel.Add(new Test() {Total=0,Options=new ObservableCollection<string>() {"--","option1","option2" } });
        ViewModel.Add(new Test() { Total = 0, Options = new ObservableCollection<string>() {"--" ,"option1", "option2" } });
    }

    public ObservableCollection<Test> ViewModel { get; set; }
}

您可以在 DescriptionCode 属性的设置器中实现您的逻辑,而不是处理事件,例如:

private string _description;
public string Description
{
    get { return _description; }
    set
    {
        SetProperty(ref _description, value);
        //your "selection changed" logic goes here...
    }
}

正如@Xavier Xie 建议的那样,定义 DescriptionCodePriceQuantity 等属性的 class 应该实现INotifyPropertyChanged 界面并引发更改通知以自动刷新视图。请参阅 MSDN 了解更多相关信息。

由于您使用的是 Prism,因此您可以继承 Prism.Mvvm.BindableBase 并调用 SetProperty<T> 方法来为 属性.

设置和引发更改通知