2

我有相同數據綁定(從XML)兩個列表選擇器,當第一listpicker有選擇改變它應該過濾其中在第一listpicker選擇的第二listpicker和隱藏項的數據,相同的第二listpicker。更新和Filter listpicker

2 listpicker XAML ...

<my:ListPicker HorizontalAlignment="Left" 
         x:Name="listPicker1" Width="265" BorderBrush="{x:Null}" FontFamily="Segoe UI" FontSize="18.667" Background="{StaticResource PhoneTextBoxBrush}" ScrollViewer.VerticalScrollBarVisibility="Visible" Margin="147,0,0,0" Grid.Row="1" Foreground="#FF1BA1E2" Height="35" SelectionChanged="listPicker1_SelectionChanged" > 
         <my:ListPicker.ItemTemplate> 
          <DataTemplate> 
           <StackPanel Width="360" Height="34"> 
            <TextBlock x:Name="item" Text="{Binding ChannelName, Mode=TwoWay}" FontSize="18.667" Margin="12, 0, 2, 2" /> 
           </StackPanel> 
          </DataTemplate> 
         </my:ListPicker.ItemTemplate> 
        </my:ListPicker> 
        <TextBlock TextWrapping="Wrap" FontFamily="Segoe UI" FontSize="16" Margin="52,5,0,5" Grid.Row="3" HorizontalAlignment="Left" Width="91" Text="Channel 2 "/> 
        <my:ListPicker HorizontalAlignment="Left" 
         x:Name="listPicker2" Width="265" BorderBrush="{x:Null}" FontFamily="Segoe UI" FontSize="18.667" Background="{StaticResource PhoneTextBoxBrush}" Foreground="#FF1BA1E2" ScrollViewer.VerticalScrollBarVisibility="Visible" Margin="147,0,0,0" Grid.Row="3" SelectionChanged="listPicker2_SelectionChanged" > 
         <my:ListPicker.ItemTemplate> 
          <DataTemplate> 
           <StackPanel Width="360" Height="34"> 
            <TextBlock x:Name="item" Text="{Binding ChannelName, Mode=TwoWay}" FontSize="18.667" Margin="12, 0, 2, 2" /> 
           </StackPanel> 
          </DataTemplate> 
         </my:ListPicker.ItemTemplate> 
        </my:ListPicker> 

數據綁定使用XML代碼

public Customize() 
    { 
     InitializeComponent(); 

     XDocument loadedData = XDocument.Load("newsChannels.xml"); 

     var channels = from query in loadedData.Descendants("channel") 
         select new Channels 
         { 
          ChannelName = (string)query.Element("channelname"), 
         }; 
     listPicker1.ItemsSource = channels; 
     listPicker2.ItemsSource = channels; 
    } 

    public class Channels 
    { 
     string channelname; 

     public string ChannelName 
     { 
      get { return channelname; } 
      set { channelname = value; } 
     } 
    } 

回答

1

而不是數據綁定的ListPickers完全相同的列表嘗試創建一個控制訪問底層表2代理屬性。然後,您可以有getter是否代理屬性過濾掉任何已被選定爲其他列表(假設sleected對象也綁定到視圖模型。或者(或可能除了),你可以使用SelectionChanged事件迫使更新。到代理列表

更新 下面是一個例子:

假設一個頁面包含此:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
    <StackPanel> 
     <toolkit:ListPicker x:Name="picker1" 
          ItemsSource="{Binding List1, Mode=TwoWay}" 
          SelectedItem="{Binding SelectedItem1, Mode=TwoWay}" 
          SelectionChanged="ListPicker1SelectionChanged" /> 
     <toolkit:ListPicker x:Name="picker2" 
          ItemsSource="{Binding List2, Mode=TwoWay}" 
          SelectedItem="{Binding SelectedItem2, Mode=TwoWay}" 
          SelectionChanged="ListPicker2SelectionChanged" /> 
    </StackPanel> 
</Grid> 

背後的代碼應該是這樣的:

public partial class MainPage : PhoneApplicationPage 
{ 
    public MainPage() 
    { 
     InitializeComponent(); 

     this.DataContext = new TwoListViewModel(); 
    } 

    private void ListPicker1SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     // Ensure that the selected Item is updated 
     picker1.GetBindingExpression(ListPicker.SelectedItemProperty).UpdateSource(); 

     // rebind the other list 
     var binding = picker2.GetBindingExpression(ListPicker.ItemsSourceProperty).ParentBinding; 
     picker2.SetBinding(ListPicker.ItemsSourceProperty, binding); 
    } 

    private void ListPicker2SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     picker2.GetBindingExpression(ListPicker.SelectedItemProperty).UpdateSource(); 

     var binding = picker1.GetBindingExpression(ListPicker.ItemsSourceProperty).ParentBinding; 
     picker1.SetBinding(ListPicker.ItemsSourceProperty, binding); 
    } 
} 

public class TwoListViewModel 
{ 
    public TwoListViewModel() 
    { 
     // MUST Initialize the selected items 
     SelectedItem1 = "one"; 
     SelectedItem2 = "two"; 
    } 

    private IEnumerable<string> InnerList 
    { 
     get 
     { 
      return new[] 
        { 
         "one", 
         "two", 
         "three", 
         "four", 
         "five", 
        }; 
     } 
    } 

    public IEnumerable<string> List1 
    { 
     get 
     { 
      return InnerList.Where(item => item != SelectedItem2); 
     } 
    } 

    public IEnumerable<string> List2 
    { 
     get 
     { 
      return InnerList.Where(item => item != SelectedItem1); 
     } 
    } 

    public string SelectedItem1 { get; set; } 

    public string SelectedItem2 { get; set; } 
} 
+0

感謝好友的回答..抱歉,但林不WP7的專業開發人員,即時對WP7應用程序開發新的,如果u好心幫我寫這個代碼.. –

+0

謝謝哥們..感謝alottt,我得到它.. –

+0

馬特,另一個問題我要問,是他們的任何可能性組選定的項目爲無?,在listpicker它顯示爲空。我搜索上,但大多數的結果,我發現他們說的不是更多鈔票,但他們給它一個解決方案「崩潰」 –