关于过滤Listpicker的指导

时间:2011-08-09 20:02:43

标签: c# silverlight windows-phone-7

我有4个带有HeaderName的ListPickers(计算机配件类别)。

  1. 处理器。
  2. 主板。
  3. RAM。
  4. 硬盘。

    Categories.XAML.CS

    public Categories()
    {
        InitializeComponent();
        this.DataContext = new manufactuers();
    }
    
    private void processorListPicker_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        processorListPicker.GetBindingExpression(ListPicker.SelectedItemProperty).UpdateSource();
    }
    
    private void motherboardListPicker_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        motherboardListPicker.GetBindingExpression(ListPicker.SelectedItemProperty).UpdateSource();
    }
    
    private void harddriveListPicker_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        harddriveListPicker.GetBindingExpression(ListPicker.SelectedItemProperty).UpdateSource();
    }
    
    private void ramListPicker_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        ramListPicker.GetBindingExpression(ListPicker.SelectedItemProperty).UpdateSource();
    }
    


  5. Categories.XAML

            <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <toolkit:ListPicker x:Name="processorListPicker" Header="Processor"
                ItemsSource="{Binding List1, Mode=TwoWay}"
                SelectedItem="{Binding SelectedItem1, Mode=TwoWay}" 
                SelectionChanged="processorListPicker_SelectionChanged" Width="360" Margin="54,106,42,0" VerticalAlignment="Top" />
            <toolkit:ListPicker x:Name="motherboardListPicker" Header="Motherboard"
                ItemsSource="{Binding List2, Mode=TwoWay}"
                SelectedItem="{Binding SelectedItem2, Mode=TwoWay}"
                SelectionChanged="motherboardListPicker_SelectionChanged" Width="360" Margin="54,191,42,0" VerticalAlignment="Top" />
            <toolkit:ListPicker x:Name="harddriveListPicker" Header="HardDrive"
                ItemsSource="{Binding List3, Mode=TwoWay}"
                SelectedItem="{Binding SelectedItem3, Mode=TwoWay}"
                SelectionChanged="harddriveListPicker_SelectionChanged" Width="360" Margin="54,282,42,240" Height="85" />
            <toolkit:ListPicker x:Name="ramListPicker" Header="RAM"
                ItemsSource="{Binding List4, Mode=TwoWay}"
                SelectedItem="{Binding SelectedItem4, Mode=TwoWay}"
                SelectionChanged="ramListPicker_SelectionChanged" Width="360" Margin="54,0,42,155" VerticalAlignment="Bottom" Height="85" />
        </Grid>
    


    。制造商.s - ViewModel

    public class manufacturers
    {
        public manufacturers()
        {
            // MUST Initialize the selected items
            SelectedItem1 = "INTEL";
            SelectedItem2 = "ASUS";
            SelectedItem3 = "WESTERN DIGITAL";
            SelectedItem4 = "CORSAIR";
        }
    
        private IEnumerable<string> manufacturersList
        {
            get
            {
                return new[]
                   {
                       "INTEL",
                       "AMD",
                       "WESTERN DIGITAL",
                       "CORSAIR",
                       "SEAGATE",
                       "ASUS",
                       "SAMSUNG",
                       "TOSHIBA",
                       "KINGSTON",
                   };
            }
        }
    
        public IEnumerable<string> List1
        {
            get
            {
                return manufacturersList;
            }
        }
    
        public IEnumerable<string> List2
        {
            get
            {
                return manufacturersList;
            }
        }
    
        public IEnumerable<string> List3
        {
            get
            {
                return manufacturersList;
            }
        }
    
        public IEnumerable<string> List4
        {
            get
            {
                return manufacturersList;
            }
        }
    
        public string SelectedItem1 { get; set; }
        public string SelectedItem2 { get; set; }
        public string SelectedItem3 { get; set; }
        public string SelectedItem4 { get; set; }
    }
    

    现在,当用户点击任何这些LISTPICKERS时,例如用户选择PROCESSORS-LISTPICKER,制造处理器的所有供应商都应该显示在LISTPICKER项目中。

    注意:假设我们有制造商列表,可以从viewmodel或在线解析XML或HTML。

2 个答案:

答案 0 :(得分:1)

这对我来说是新的,因为我不是一个经验丰富的程序员,所以我不能给你代码,所以我必须研究如何构建它,但我会这样做的方式是绑定到视图模型的控件,并使用INotifyPropertyChanged将该视图模型发送回UI。将按钮(我假设按钮,因为您说单击它们)绑定到视图模型中的命令,视图模型使用该事件将正确的列表返回到ListBox。

我现在正在尝试自学MVVM,否则我甚至无法提出建议。

答案 1 :(得分:0)

除非我不理解这个问题:

XAML:

 <Controls:ListPicker Header="Processor:"
                             ItemsSource="{Binding ProcessorMfgs}"
                             Margin="0,499,0,-499" />
ViewModel中的

  private List<string> _processorMfgs;
        public List<string> ProcessorMfgs
        {
            get { return _processorMfgs; }
            set
            {
                _processorMfgs = value;
                NotifyOfPropertyChange(() => ProcessorMfgs);
            }
        }

    ProcessorMfgs = new List<string>
                                {
                                    "Intel", "AMD"
                                };