ListPicker数据绑定和INotifyPropertyChanged

时间:2015-07-11 00:30:45

标签: c# silverlight windows-phone-7 windows-phone-8 windows-phone-8.1

所以我有两个ListPickers,Device TypeDevice Name

如果我在Device Type中选择 Tablet ,我希望Device Name ListPicker显示 Ipad Dell Venue 8 <等选项/ em>等。

如果我在Device Type中选择手机,我希望Device Name ListPicker显示 Iphone Samsung Galaxy 等选项em>等等。

那么我如何才能在这两个ListPickers之间建立数据绑定并实现INotifyPropertyChanged,以便一个ListPicker中的更改动态地反映在另一个中?

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

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

此外,我会让你更改变量和类型名称,因为你没有提供任何代码样本!