ListPicker数据绑定和INotifyPropertyChanged

本文关键字:INotifyPropertyChanged 数据绑定 ListPicker | 更新日期: 2023-09-27 18:02:15

所以我有两个ListPickers, Device TypeDevice Name

如果我在Device Type中选择Tablet,我希望Device Name ListPicker显示IpadDell Venue 8等选项。

如果我在Device Type中选择Phone,我希望Device Name的ListPicker显示像IphoneSamsung Galaxy等选项。

那么我该如何在这两个ListPicker之间进行数据绑定,并实现INotifyPropertyChanged,以便一个ListPicker中的更改动态反映在另一个ListPicker中?

ListPicker数据绑定和INotifyPropertyChanged

您可以这样做:

在你的xaml:

<toolkit:ListPicker x:Name="DeviceType" ItemSource="{Binding DeviceTypeList}" SelectedItem="{Binding SelectedDeviceType, Mode=TwoWay}"/>
<toolkit:ListPicker x:Name="DeviceName" ItemSource="{Binding DeviceNameList}" />

在你的代码中:

public class ClassName : NotifyChangements
{
    private YourType selectedDeviceType;
    public YourType SelectedDeviceType
    {
        get { return selectedDeviceType; }
        set
        {
            selectedDeviceType = value;
            NotifyPropertyChanged("SelectedDeviceType");
            MAJDeviceName();
        }
    }
    // Later in code.
    public void MAJDeviceName()
    {
        // Add code here that fill the DeviceNameList according to the SelectedDeviceType.
    }
}

对于NotifyChangements类:

using System.ComponentModel;
using System.Runtime.CompilerServices;
public class NotifyChangements : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
        public bool NotifyPropertyChanged<T>(ref T variable, T valeur, [CallerMemberName] string property = null)
        {
            if (object.Equals(variable, valeur)) return false;
            variable = valeur;
            NotifyPropertyChanged(property);
            return (true);
        }
    }

您还必须将List<YourType> DeviceNameList添加为属性,并在该属性的setter中调用NotifyPropertyChanged("DeviceNameList"),以使其绑定数据。

此外,我将允许您更改变量和类型名称,因为您没有提供任何代码示例!