繁体   English   中英

ListPicker数据绑定和INotifyPropertyChanged

[英]ListPicker Data Binding and INotifyPropertyChanged

所以我有两个ListPickers, Device TypeDevice Name

如果我在“ Device Type选择“ 平板电脑” ,则希望“ Device Name列表选择器”显示IpadDell Venue 8等选项。

如果我在“ Device Type选择“ 电话” ,则希望“ Device Name列表选择器”显示诸如IphoneSamsung Galaxy等之类的选项。

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

您可以执行以下操作:

在您的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") ,以使其绑定数据。

而且,由于您没有提供任何代码示例,因此我将让您更改变量和类型名称!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM