【问题标题】:WPF: Enabling a dropdown based on the value on another dropdownWPF:根据另一个下拉菜单上的值启用下拉菜单
【发布时间】:2017-01-16 09:25:51
【问题描述】:

我的视图中有两个组合框。

我想要的是,当我在 ComboBox1 上选择值 1 时,ComboBox2 变为启用状态。但是,当我在 ComboBox1 中选择 1 以外的任何其他值时,ComboBox2 将保持禁用状态。

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    您的 XAML:

    <ComboBox x:Name="cb1" Margin="10,0" IsReadOnly="True" IsEditable="True" Text="Please select" HorizontalAlignment="Center" >
        <ComboBoxItem x:Name="cbi11" Content="Option1" HorizontalAlignment="Left" Width="198" Selected="cbi11_Selected" />
        <ComboBoxItem x:Name="cbi12" Content="Option2" HorizontalAlignment="Left" Width="198" Selected="cbi12_Selected"/>
        <ComboBoxItem x:Name="cbi13" Content="Option3" HorizontalAlignment="Left" Width="198" Selected="cbi13_Selected"/>
    </ComboBox>
    
    <ComboBox x:Name="cb2" Margin="10,0" IsReadOnly="True" IsEditable="True" Text="Please select" HorizontalAlignment="Center" >
        <ComboBoxItem x:Name="cbi21" Content="Option1" HorizontalAlignment="Left" Width="198" Selected="cbi21_Selected" />
        <ComboBoxItem x:Name="cbi22" Content="Option2" HorizontalAlignment="Left" Width="198" Selected="cbi22_Selected"/>
        <ComboBoxItem x:Name="cbi23" Content="Option3" HorizontalAlignment="Left" Width="198" Selected="cbi23_Selected"/>
    </ComboBox>
    

    还有你的代码:

    private void cbi11_Selected(object sender, RoutedEventArgs e)
        { 
            cb2.IsEnabled = true;
        }
    private void cbi12_Selected(object sender, RoutedEventArgs e)
        { 
            cb2.IsEnabled = false;
        }
    private void cbi13_Selected(object sender, RoutedEventArgs e)
        { 
            cb2.IsEnabled = false;
        }
    

    【讨论】:

    • @Timo Kiander 是你想做的吗?
    【解决方案2】:

    如果您正确地执行此操作,那么您提出了错误的问题,因为您的视图模型将为您完成大部分工作

    假设您有这样的 ViewModel(使用 Prism

    public class VM:BindableBase
    {
        public ICollectionView Combo1Options {get,set}
        public ICollectionView Combo2Options {get,set}
    
        private object _Combo1Value;
        private object _Combo2Value;
    
        public object Combo1Value 
        {
            get { return _Combo1Value; }
            set 
            {
                if(SetProperty(ref _Combo1Value, value))
                {
                    Combo2Options .Refresh();
                    PropertyChanged(nameof(Combo2Enabled));                    
                }
            }
        }
    
        public object Combo2Value 
        {
            get { return _Combo2Value; }
            set { SetProperty(ref _Combo2Value, value); }
        }
        public bool Combo2Enabled => Combo1Value != null;//or what ever logic defines if combo2 is required
    
    }
    

    那么你只需绑定到你的视图模型

    <ComboBox ItemSource={Binding Combo1Options }, SelectedItem={Binding Combo1Value }/>
    <ComboBox ItemSource={Binding Combo2Options }, SelectedItem={Binding Combo2Value } IsEnabled={Binding Combo2Enabled}/>
    

    通过使用 CollectionView,您可以获得过滤组合框项目的能力,所以我会推荐它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多