【问题标题】:WPF - Enable 2nd combobox on 1st combobox selectionWPF - 在第一个组合框选择上启用第二个组合框
【发布时间】:2016-08-31 23:14:50
【问题描述】:

我有两个 ComboBox,我想在选择第一个 ComboBox 时启用第二个 ComboBox。我尝试添加 IsEnabled 属性,但似乎不起作用。我试过的代码如下。

<dxe:ComboBoxEdit Name="siteComboBox" HorizontalAlignment="Left" Margin="97,104,0,0" 
     VerticalAlignment="Top" Width="150" ItemsSource="{Binding Site}" 
     SelectedItem="{Binding SelectedSite}"/>
<dxe:ComboBoxEdit Name="planTypeComboBox" HorizontalAlignment="Left" Margin="97,159,0,0" 
     VerticalAlignment="Top" Width="150" 
     ItemsSource="{Binding PlanType}" SelectedItem="{Binding SelectedPlanType}" 
     IsEnabled="{Binding ElementName=siteComboBox}"/>

谁能指出我做错了什么?或者还有其他方法吗?

【问题讨论】:

    标签: c# .net wpf mvvm combobox


    【解决方案1】:

    ComboBox 没有 IsChecked 属性。所以这种绑定是行不通的。

    您可以设置另一个属性,例如 IsSiteSelected,当 SelectedSite 不为 null 时返回 true 并改为绑定到该属性。

    【讨论】:

    • 感谢您的建议 :)
    【解决方案2】:

    当第一个组合框的 SelectedIndex 不为 -1 时,为第二个组合框设置 IsEnabled 或者为第一个组合框定义一个 SelectionChanged 事件并在后端启用/禁用第二个组合框

    【讨论】:

      【解决方案3】:

      您可以使用数据触发器。当所选项目为空时,第二个 ComboBox 将被禁用

      <dxe:ComboBoxEdit Name="siteComboBox" HorizontalAlignment="Left" Margin="97,104,0,0" 
           VerticalAlignment="Top" Width="150" ItemsSource="{Binding Site}" 
           SelectedItem="{Binding SelectedSite}"/>
      <dxe:ComboBoxEdit Name="planTypeComboBox" HorizontalAlignment="Left" Margin="97,159,0,0" 
           VerticalAlignment="Top" Width="150" 
           ItemsSource="{Binding PlanType}" SelectedItem="{Binding SelectedPlanType}">
           <dxe:ComboBoxEdit.Style>
               <Style TargetType="{x:Type dxe:ComboBoxEdit}">
                   <Setter Property="IsEnabled" Value="True"/>
                   <Style.Triggers>
                       <DataTrigger Binding="{Binding ElementName=siteComboBox, Path=SelectedItem}" Value="{x:Null}">
                           <Setter Property="IsEnabled" Value="False"/>
                       </DataTrigger>
                   </Style.Triggers>
               </Style>
           </dxe:ComboBoxEdit.Style>
      </dxe:ComboBoxEdit>
      

      编辑:如果使用隐式主题,定义的样式必须继承自主题样式:

      <dxe:ComboBoxEdit Name="siteComboBox" HorizontalAlignment="Left" Margin="97,104,0,0" 
           VerticalAlignment="Top" Width="150" ItemsSource="{Binding Site}" 
           SelectedItem="{Binding SelectedSite}"/>
      <dxe:ComboBoxEdit Name="planTypeComboBox" HorizontalAlignment="Left" Margin="97,159,0,0" 
           VerticalAlignment="Top" Width="150" 
           ItemsSource="{Binding PlanType}" SelectedItem="{Binding SelectedPlanType}">
           <dxe:ComboBoxEdit.Style>
               <Style TargetType="{x:Type dxe:ComboBoxEdit}" BasedOn="{StaticResource {x:Type dxe:ComboBoxEdit}}">
                   <Setter Property="IsEnabled" Value="True"/>
                   <Style.Triggers>
                       <DataTrigger Binding="{Binding ElementName=siteComboBox, Path=SelectedItem}" Value="{x:Null}">
                           <Setter Property="IsEnabled" Value="False"/>
                       </DataTrigger>
                   </Style.Triggers>
               </Style>
           </dxe:ComboBoxEdit.Style>
      </dxe:ComboBoxEdit>
      

      【讨论】:

      • 谢谢你的回答:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-27
      • 2018-11-07
      相关资源
      最近更新 更多