【问题标题】:ListPicker Data Binding and INotifyPropertyChangedListPicker 数据绑定和 INotifyPropertyChanged
【发布时间】:2015-07-11 00:30:45
【问题描述】:

所以我有两个 ListPicker,Device TypeDevice Name

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

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

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

【问题讨论】:

  • 看看How to Ask。如果您有一些示例代码,它会有所帮助。
  • 绑定两个组合是一项非常常见的任务,并且有很多重复项。甚至 WPF 示例也适用,因为模式仍然完全相同。

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


【解决方案1】:

您可以执行以下操作:

在您的 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&lt;YourType&gt; DeviceNameList 添加为属性并在该属性的设置器中调用NotifyPropertyChanged("DeviceNameList") 以使其绑定数据。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-31
    • 2011-07-04
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多