代码之家  ›  专栏  ›  技术社区  ›  nuuse

根据第二个组合框的选择隐藏组合框项,反之亦然

  •  1
  • nuuse  · 技术社区  · 8 年前

    我有两个组合框,每个都是绑定的(!)同样的 ObservableCollection<string> . 我想防止选择相同的项目。

    这是我的C代码(firstload bool只是为了防止在第一次加载函数时执行):

    private void comboBoxFilter1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (!firstload)
        {
            for (int i = 0; i <= comboBoxFilter2.Items.Count - 1; i++)
            {
                if ((((ComboBoxItem)(comboBoxFilter2.Items[i])).Content as string) == (((ComboBoxItem)comboBoxFilter1.SelectedItem).Content as string))
                // This is where I get the InvalidCaseException ^
                {
                    (comboBoxFilter2.Items[i] as ComboBoxItem).Visibility = System.Windows.Visibility.Collapsed;
                    //and on this line the nullreferenceException, in particular, although the Item[i] does have Value!
                }
                else
                {
                    (comboBoxFilter2.Items[i] as ComboBoxItem).Visibility = System.Windows.Visibility.Visible;
                    //and on this line the nullreferenceException, in particular, although the Item[i] does have Value!
                }
            }
        }
    }
    
    private void comboBoxFilter2_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {            
        if (!firstload)
        {
            for (int i = 0; i <= comboBoxFilter1.Items.Count - 1; i++)
            {
                if ((((ComboBoxItem)(comboBoxFilter1.Items[i])).Content as string) == (((ComboBoxItem)comboBoxFilter2.SelectedItem).Content as string))
                {
                    (comboBoxFilter1.Items[i] as ComboBoxItem).Visibility = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    MessageBox.Show((comboBoxFilter2.Items[i] as ComboBoxItem).Visibility.ToString());
                    (comboBoxFilter1.Items[i] as ComboBoxItem).Visibility = System.Windows.Visibility.Visible;
                }
            }
        }
    
        firstload = false;
    }
    

    这是我的Xaml:

    <ComboBox x:Name="comboBoxFilter1" 
              Grid.Column="0" 
              Grid.Row="2"     
              HorizontalAlignment="Stretch" 
              VerticalAlignment="Bottom"     
              SelectionChanged="comboBoxFilter1_SelectionChanged" 
              SelectedIndex="0"     
              Visibility="Visible"/>    
    
    <ComboBox x:Name="comboBoxFilter2" 
              Grid.Column="1" Grid.Row="2"     
              HorizontalAlignment="Stretch" 
              VerticalAlignment="Bottom"     
              SelectionChanged="comboBoxFilter2_SelectionChanged" 
              SelectedIndex="1"    
              Visibility="Visible"/>    
    

    注意,我在代码中执行Itemsource,而不是在Xaml中。

    NullReferenceExecption 或者 InvalidCastException (参见代码中的注释)。同样的错误也发生在 comboBoxFilter2_SelectionChange

    1 回复  |  直到 6 年前
        1
  •  3
  •   Sinatr    8 年前

    <StackPanel>
        <ComboBox ItemsSource="{Binding List1}" SelectedItem="{Binding Selected1}" />
        <ComboBox ItemsSource="{Binding List2}" SelectedItem="{Binding Selected2}" />
    </StackPanel>
    

    public class ViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string property = "") => 
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
    
        readonly List<string> _list = new List<string> { "a", "b", "c", "d", "e" };
        public IEnumerable<string> List1 => _list.Where(o => o != Selected2);
        public IEnumerable<string> List2 => _list.Where(o => o != Selected1);
    
        string _selected1;
        public string Selected1
        {
            get { return _selected1; }
            set
            {
                _selected1 = value;
                OnPropertyChanged();
                OnPropertyChanged(nameof(List2));
            }
        }
    
        string _selected2;
        public string Selected2
        {
            get { return _selected2; }
            set
            {
                _selected2 = value;
                OnPropertyChanged();
                OnPropertyChanged(nameof(List1));
            }
        }
    }
    

    注意:当视图更改所选项目时,viewmodel只会触发 NotifyPropertyChanged 评估绑定中使用的属性及其值的事件。

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new ViewModel() { Selected1 = "a", Selected2 = "d" };
        }
    }
    

    推荐文章