C# WPF-在选择第一个组合框时启用第二个组合框

C# WPF-在选择第一个组合框时启用第二个组合框,c#,.net,wpf,mvvm,combobox,C#,.net,Wpf,Mvvm,Combobox,我有两个组合框,我想在选择第一个组合框时启用第二个组合框。我尝试添加IsEnabled属性,但似乎不起作用。我试过的代码如下 <dxe:ComboBoxEdit Name="siteComboBox" HorizontalAlignment="Left" Margin="97,104,0,0" VerticalAlignment="Top" Width="150" ItemsSource="{Binding Site}" SelectedItem="{Binding

我有两个组合框,我想在选择第一个组合框时启用第二个组合框。我尝试添加
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}"/>


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

组合框没有被选中的属性。这样绑定就不起作用了


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

A
组合框
没有被选中的
属性。这样绑定就不起作用了


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

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

您可以使用DataTriggers。当所选项目为空时,第二个组合框将被禁用

<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>

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

<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>


谢谢你的建议:)谢谢你的建议:)谢谢你的回答:)谢谢你的回答:)