禁用groupBox,包括WPF中的groupBox名称

时间:2013-11-22 11:28:17

标签: c# wpf groupbox

我有一个groupBox名称“groupBox”。我想禁用整个groupbox,包括组框的名称。 我正在附上图片。我希望它能清除情况enter image description here

<GroupBox Name="groupBox"  Grid.Column="0" Grid.Row="1" Margin="2,0,0,0" Header="GroupBox" IsEnabled="False">
            <Grid Margin="10,0,0,0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition></ColumnDefinition>

                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height=".250*"/>
                    <RowDefinition Height=".250*"/>
                </Grid.RowDefinitions>
                <RadioButton Name="RadioBtn1" VerticalAlignment="Center"  ToolTipService.ShowOnDisabled="True" Grid.ColumnSpan="3" Height="14.63">OP1</RadioButton>
                <RadioButton Name="RadioBtn2" Grid.Row="1"  VerticalAlignment="Center"  ToolTipService.ShowOnDisabled="True" Grid.ColumnSpan="3" Height="14.63">OP2</RadioButton>

            </Grid>
        </GroupBox>

我正在使用 IsEnabled =“False”属性。但它只适用于groupBox的内容(如果我错了,请给我看看!) 现在我希望圈区也应该禁用。

4 个答案:

答案 0 :(得分:5)

一个简单的Trigger可以在XAML中为您执行此操作(不包含任何代码):

<GroupBox Name="groupBox" Grid.Column="0" Grid.Row="1" Margin="2,0,0,0" Header="GroupBox" IsEnabled="False">
    <GroupBox.Style>
        <Style>
            <Style.Triggers>
                <Trigger Property="Control.IsEnabled" Value="False">
                    <Setter Property="Control.Foreground" Value ="#FF6D6D6D" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </GroupBox.Style>
    <Grid Margin="10,0,0,0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height=".250*"/>
            <RowDefinition Height=".250*"/>
        </Grid.RowDefinitions>
        <RadioButton Name="RadioBtn1" VerticalAlignment="Center"  ToolTipService.ShowOnDisabled="True" Grid.ColumnSpan="3" Height="14.63">OP1</RadioButton>
        <RadioButton Name="RadioBtn2" Grid.Row="1"  VerticalAlignment="Center"  ToolTipService.ShowOnDisabled="True" Grid.ColumnSpan="3" Height="14.63">OP2</RadioButton>
    </Grid>
</GroupBox>

答案 1 :(得分:2)

只需使用触发器

<GroupBox Name="groupBox" Grid.Column="0" Grid.Row="1" Margin="2,0,0,0" Header="GroupBox" IsEnabled="False">
  <Grid Margin="10,0,0,0">
    <Grid.ColumnDefinitions>
      <ColumnDefinition>
      </ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition Height=".25*"/>
      <RowDefinition Height=".25*"/>
    </Grid.RowDefinitions>
    <RadioButton Name="RadioBtn1" Height="14.63" Grid.ColumnSpan="3" VerticalAlignment="Center" ToolTipService.ShowOnDisabled="True">OP1
    </RadioButton>
    <RadioButton Name="RadioBtn2" Height="14.63" Grid.ColumnSpan="3" Grid.Row="1" VerticalAlignment="Center" ToolTipService.ShowOnDisabled="True">OP2
    </RadioButton>
  </Grid>
  <GroupBox.Style>
    <Style TargetType="GroupBox">
      <Setter Property="HeaderTemplate">
        <Setter.Value>
          <DataTemplate>
            <TextBlock x:Name="header" Text="{Binding}"/>
            <DataTemplate.Triggers>
              <Trigger Property="IsEnabled" Value="False">
                <Setter TargetName="header" Property="Foreground" Value="Gray"/>
              </Trigger>
            </DataTemplate.Triggers>
          </DataTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </GroupBox.Style>
</GroupBox>

答案 2 :(得分:0)

你可以改变颜色:

<GroupBox>
    <GroupBox.Header>
      <Label Foreground="Gray">My Group Header Label</Label>
    </GroupBox.Header>
</GroupBox>

您可以使用转换器更改颜色:

<Window.Resources>
            <local:BrushColorConverter x:Key="BConverter"></local:BrushColorConverter>
</Window.Resources>
...
<GroupBox>
    <GroupBox.Header>
      <Label Foreground="{Binding Path=IsEnabled, Converter={StaticResource BConverter}}">My Group Header Label</Label>
    </GroupBox.Header>
</GroupBox>

转换器代码

 public class BrushColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((bool)value)
            {
                {
                    return System.Windows.Media.Colors.Gray;
                }
            }
            return System.Windows.Media.Colors.Black;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

    }

答案 3 :(得分:0)

实际上,根本不需要使用触发器或绑定。您可以简单地使用Label作为标头(由@progpow建议),标头会像其他任何控件一样变成灰色:

<GroupBox>
    <GroupBox.Header>
        <Label Content="My Header Text" Padding="0" />
    </GroupBox.Header>
</GroupBox>

如果仅将字符串分配给Header属性,则WPF将使用TextBlock来可视化标题,但是由于TextBlock只是FrameworkElement而不是Control类似于Label,它不支持被禁用(请参见Differences between Label and TextBlock)。

有必要为Padding="0"定义零填充(Label),因为默认填充为5,有点像GroupBox的标头。