如何根据组合框选择禁用wpf中的多个元素?

时间:2018-09-25 09:29:48

标签: c# wpf xaml user-interface

我需要基于另一个ComboBox选择禁用LabelComboBox

我有以下代码来禁用组合框,但是我还需要禁用组合框的标签。

XAML

<Label  Style="{StaticResource LabelTitle}"   Content="F"  x:Name="Label_FType" Margin="25.087,24.592,-94.145,7.6" d:LayoutOverrides="Width, Height"  />
<ComboBox x:Name="Combo_F_Type" Style="{StaticResource ComboBox}" IsSynchronizedWithCurrentItem="True" Margin="46.145,29.692,0,12.5" Grid.Column="1" 
          Width="226.199" HorizontalAlignment="Left" d:LayoutOverrides="Height">
    <ComboBoxItem Content="Select Type" IsSelected="True" />
    <ComboBoxItem Content="F1" />
    <ComboBoxItem Content="F2" />
    <ComboBoxItem Content="F3" />
</ComboBox> 

<Label Style="{StaticResource LabelTitle}" Content="S" x:Name="Label_SType" Margin="25.087,3.1,-94.145,29.092" d:LayoutOverrides="Width, Height" Grid.Row="1" />
<ComboBox Style="{StaticResource EnableSType}" IsSynchronizedWithCurrentItem="True" Margin="46.145,8.2,0,33.992" Grid.Column="1" Width="226.199"
          HorizontalAlignment="Left" d:LayoutOverrides="Height" Grid.Row="1" >
   <ComboBoxItem Content="Select Type" IsSelected="True" />
   <ComboBoxItem Content="S1" />
   <ComboBoxItem Content="S2" />
   <ComboBoxItem Content="S3" />
   <ComboBoxItem Content="S4" />
</ComboBox> 

XAML样式

<Style TargetType="{x:Type ComboBox}" x:Key="EnableSType">
    <Setter  Property="FontSize" Value="14" />
    <Setter Property="Height" Value="30"/>
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="IsEnabled" Value="False" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding ElementName=Combo_FType, Path=SelectedIndex}" Value="3">
            <Setter Property="IsEnabled" Value="True" />
        </DataTrigger>
    </Style.Triggers>
</Style>

有人可以帮我吗?

2 个答案:

答案 0 :(得分:1)

您可以将Labels IsEnabled属性绑定到ComboBox的IsEnabled属性。

<Label IsEnabled="{Binding ElementName=Combo_S_Type, Path=IsEnabled}" Style="{StaticResource LabelTitle}"   Content="S" x:Name="Label_SType" Margin="25.087,3.1,-94.145,29.092" d:LayoutOverrides="Width, Height" Grid.Row="1"  />
       <ComboBox x:Name="Combo_S_Type" Style="{StaticResource EnableSType}" IsSynchronizedWithCurrentItem="True" Margin="46.145,8.2,0,33.992" Grid.Column="1" Width="226.199" HorizontalAlignment="Left" d:LayoutOverrides="Height" Grid.Row="1" >
           <ComboBoxItem Content="Select Type" IsSelected="True" />
           <ComboBoxItem Content="S1" />
           <ComboBoxItem Content="S2" />
           <ComboBoxItem Content="S3" />
           <ComboBoxItem Content="S4" />
      </ComboBox> 

答案 1 :(得分:0)

您可以添加一个继承基本标签样式LabelTitle的新样式,它应该是这样的

XAML样式

<Style x:Key="EnableSTypeLabel" BasedOn="{StaticResource LabelTitle}" TargetType="Label">
    <Style.Triggers>
        <DataTrigger Binding="{Binding ElementName=Combo_FType, Path=SelectedIndex}" Value="3">
            <Setter Property="IsEnabled" Value="True" />
        </DataTrigger>
    </Style.Triggers>
</Style>

XAML

<Label Style="{StaticResource EnableSTypeLabel}" Content="S" x:Name="Label_SType" Margin="25.087,3.1,-94.145,29.092" d:LayoutOverrides="Width, Height" Grid.Row="1"  />