我希望我的第二个组合框不显示第一个选择项

时间:2014-06-01 10:29:42

标签: c# wpf xaml mvvm combobox

你好我有一个组合框,我从我的数据库填充它,组合框显示我的数据库中的所有值。现在我在这个之后还有一个组合框,我不想向我展示第一个选择项目。我如何存档这个?

查看:

  <ComboBox VerticalAlignment="Center" 
            Grid.Column="1" Grid.Row="0" IsEditable="False"
            IsSynchronizedWithCurrentItem="false" 
            ItemsSource="{Binding combo}" 
            SelectedItem="{Binding selectedTeam}" 
            ItemTemplate="{StaticResource DataTemp}" />

我尝试将第二个绑定到同一个Observer的集合中,并使用转换器 返回value.Visibility.Hidden; ,但它没用。

//////////////更新1 ///////////////////////

对你的回答很好,非常感谢,我已经尝试了你的方法,但现在我看不出两个组合框而不是选择项...

<ComboBox ItemsSource="{Binding}"
DataContext="{Binding ComboFilterCollection}"
ItemTemplate="{StaticResource DataTemp}" />

myviewModel:

public TestClassVM SelectedItem { get{ return _SelectedItem; }
set { _SelectedItem = value;
OnPropertyChanged("SelectedItem");

if ( _SelectedItem != null )
{
ICollectionView ComboFilterCollection = CollectionViewSource.GEtDefaultView(combo);
ComboFilterCollection.Filter = (i) => i != SelectedItem;
ComboFilterCollection.Refresh();
}
else
...
}

public ICollectionView ComboFilterCollection
{ get { return _ComboFilterCollection;}
  set { _ComboFilterCollection = value;
OnPropertyChanged("ComboFilterCollection");}}

现在,当我在我的第一个组合框中选择一些内容时,我无法在两个组合框中看到所选的值......但这怎么可能呢?我尝试了不同的方法,但它没有成功......

有人可以进一步了解将要发生的事情,感谢先生们!

1 个答案:

答案 0 :(得分:0)

ICollectionView是您的选择

这是一个例子

我添加了ICollectionView类型的属性combo2,并将OnSelectedTeamChange添加到selectedTeam,假设combo有数据。

   public ICollectionView combo2
    {
        get { return (ICollectionView)GetValue(combo2Property); }
        set { SetValue(combo2Property, value); }
    }

    // Using a DependencyProperty as the backing store for combo2.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty combo2Property =
        DependencyProperty.Register("combo2", typeof(ICollectionView), typeof(View), new PropertyMetadata(null));


    public object selectedTeam
    {
        get { return (object)GetValue(selectedTeamProperty); }
        set { SetValue(selectedTeamProperty, value); }
    }

    // Using a DependencyProperty as the backing store for selectedTeam.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty selectedTeamProperty =
        DependencyProperty.Register("selectedTeam", typeof(object), typeof(View), new PropertyMetadata(null, OnSelectedTeamChange));

初始化集合视图并添加过滤器以从源

过滤掉selectedTeam
    public void InitView()
    {
        combo2 = CollectionViewSource.GetDefaultView(combo);
        combo2.Filter = (i) => i != selectedTeam;
    }

添加了OnSelectedTeamChange方法,以便在selectedTeam更改时刷新集合视图

    public static void OnSelectedTeamChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        (d as View).combo2.Refresh();
    }

现在你可以将这个combo2绑定到你的第二个组合,它将包含所有项目但是选择了团队