Silverlight第一个组合框过滤器是第二个组合框

时间:2011-05-12 18:11:03

标签: silverlight

我有两个组合框作为常用控件创建。加载页面时,programcbo列出程序名称;并且teamcbo列出了团队名称。我想只在选择任何程序名称时显示相关的团队名称。总之,我需要通过从第一个组合框中选择名称来过滤第二个组合框。

提前致谢。

3 个答案:

答案 0 :(得分:2)

有多种方法可以解决这个问题。

例如,如果您拥有包含“团队”集合的“程序”对象,您可以这样做,几乎所有都在XAML中:

    <StackPanel>
        <ComboBox x:Name="programCbo" ItemsSource="{Binding}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding ProgramName}"></TextBlock>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
        <ComboBox x:Name="teamCbo" ItemsSource="{Binding ElementName=programCbo, Path=SelectedItem.Teams}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding TeamName}" />
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </StackPanel>

你可以在这里看到我将第一个组合框绑定到datacontext,这是一个程序列表(我们将在下一位设置它)。将第二个组合框设置为第一个组合框的selecteditem属性,然后设置为Teams属性。这样,当第一个组合框上的选择发生变化时,数据绑定将启动并导致第二个框上的itemssource更新。

在后面的代码中,我只是构建了数据源。显然,您必须以自己的方式获取数据:

    public MainPage()
    {
        InitializeComponent();

        DataContext = new List<Program>
                               {
                                       new Program
                                           {
                                                   ProgramName = "Program 1",
                                                   Teams = new List<Team>
                                                               {
                                                                       new Team
                                                                           {
                                                                                   TeamName = "Program 1 Team 1"
                                                                           },
                                                                           new Team
                                                                               {
                                                                                       TeamName = "Program 1 Team 2"
                                                                               }
                                                               }
                                           },
                                           new Program
                                               {
                                                      ProgramName = "Program 2",
                                                   Teams = new List<Team>
                                                               {
                                                                       new Team
                                                                           {
                                                                                   TeamName = "Program 2 Team 1"
                                                                           },
                                                                           new Team
                                                                               {
                                                                                       TeamName = "Program 2 Team 2"
                                                                               }
                                                               }  
                                               }
                               };

    }

如果您没有以这样的方式访问数据,则必须在组合框上处理Selection changed事件:

(XAML)

    <StackPanel>
        <ComboBox x:Name="programCbo" SelectionChanged="programCbo_SelectionChanged">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding ProgramName}"></TextBlock>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
        <ComboBox x:Name="teamCbo">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding TeamName}" />
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </StackPanel>

注意这次,我们为SelectionChanged事件设置了一个处理程序。

(代码隐藏)

    public MainPage()
    {
        InitializeComponent();

        programCbo.ItemsSource = new List<Program>
                               {
                                       new Program
                                           {
                                                   ProgramName = "Program 1",

                                           },
                                           new Program
                                               {
                                                      ProgramName = "Program 2",

                                               }
                               };

    }

    private void programCbo_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        // get the sender
        ComboBox cb = sender as ComboBox;
        // get the selected program
        Program selectedProgram = (cb.SelectedItem as Program);
        // do some stuff to get the appropriate teams and set the other combobox's itemssource to it
        teamCbo.ItemsSource = new List<Team>
                                  {
                                          new Team
                                              {
                                                      TeamName = "My favorite team!"
                                              }
                                  };
    }

你有它。啰嗦,但希望彻底的例子:)

答案 1 :(得分:0)

您可以使用分页集合进行过滤,也可以修改与第一个组合框绑定的值在第二个组合框上绑定的可观察集合。

答案 2 :(得分:0)

XAML:

...

xmlns:local="clr-namespace:SelectedProgram">

<UserControl.Resources>
    <local:MainPageViewModel x:Key="vm" />
</UserControl.Resources>

<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource vm}}">
    <Grid.RowDefinitions>
        <RowDefinition Height="50" />
        <RowDefinition Height="350" />
    </Grid.RowDefinitions>
    <sdk:Label Content="Select Team for the Program" Grid.Row="0" Margin="20,0,0,0"/>
    <StackPanel  Orientation="Horizontal" Grid.Row="1" VerticalAlignment="Top" Margin="20,0,0,0">
        <ComboBox x:Name="programcbo"  Width="100" Height="25" 
              ItemsSource="{Binding Path=Programs, Mode=OneWay}" 
              SelectedValue="{Binding Path=SelectedTeam, Mode=TwoWay}" 
              DisplayMemberPath="Key" 
              SelectedValuePath="Value" />
        <ComboBox x:Name="teamcbo"  Width="100" Height="25" Margin="20" 
              ItemsSource="{Binding Path=SelectedTeam, Mode=OneWay}"/>
    </StackPanel>
</Grid>

在像MainPageViewModel这样的类中:

 ...
 using System.Collections.ObjectModel;
 using System.Collections.Generic;
 using System.Windows.Data;

namespace SelectedProgram
{
public class MainPageViewModel : ModelBase
{
     public MainPageViewModel()
    {
         this.Programs = GetPrograms();
    }

    private string[] _selectedTeam;
    public string[] SelectedTeam
    {
        get { return _selectedTeam; }
        set
        {
            _selectedTeam = value;
            FirePropertyChanged("SelectedTeam");
        }
    }

    private ObservableCollection<KeyValuePair<string, string[]>> _programs;
    public ObservableCollection<KeyValuePair<string, string[]>> Programs
    {
        get { return _programs; }
        set
        {
            _programs = value;
            FirePropertyChanged("Programs");
        }
    }
private ObservableCollection<KeyValuePair<string, string[]>> GetPrograms()
    {
        ObservableCollection<KeyValuePair<string, string[]>> pr = new ObservableCollection<KeyValuePair<string, string[]>>();
        pr.Add(new KeyValuePair<string, string[]>("Janitors", new string[]{ "Schwarzkopf", "Ivanov", "Smith"}));
        pr.Add(new KeyValuePair<string, string[]>("Violonists", new string[] { "Einstein", "Odinzovobulyznicof", "Onestone" }));
        pr.Add(new KeyValuePair<string, string[]>("Programers", new string[] { "Petrov", "Skeet", "Stroustrup" }));            return pr;
     }  
  }

}