2

所以我有两个ListPickers,Device TypeDevice NameListPicker数据绑定和INotifyPropertyChanged

如果我在Device Type选择平板,我希望Device Name ListPicker显示选项,如的Ipad戴尔Venue 8

如果我在Device Type选择电话,我想要的Device Name ListPicker显示选项如iphone,三星Galaxy等等。

那么我该如何去做这两个ListPickers之间的数据绑定,并且还实现了INotifyPropertyChanged,因此一个ListPicker中的更改会动态地反映到另一个ListPicker中?

+0

看看[问]。它有助于如果你有一些示例代码。 –

+0

绑定两个连击是一个非常普遍的任务,并有许多重复。即使WPF的例子也适用,因为模式仍然完全相同。 – Will

回答

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作为一个n属性,并在该属性的setter中调用NotifyPropertyChanged("DeviceNameList")以使其绑定数据。

此外,我会让你改变变量和输入名称,因为你还没有提供任何代码示例!

相关问题