问题
I have a groupBox name "groupBox".I want to disable whole groupbox includind the name of the group box. I am attaching the image.I hope it would clear the situation
<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>
I am using IsEnabled="False" property. but it work only content of groupBox(Pls currect me if I am wrong!) Now I want that the circle area should also be disable.
回答1:
A simple Trigger
can do this for you in XAML (without any code behind):
<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>
回答2:
simply using trigger
<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>
回答3:
You can change color with this:
<GroupBox>
<GroupBox.Header>
<Label Foreground="Gray">My Group Header Label</Label>
</GroupBox.Header>
</GroupBox>
You can change color with converters:
<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>
Converter code
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();
}
}
回答4:
Actually it is not necessary to use triggers or bindings at all. You can simply use a Label
as header (as suggested by @progpow) and the header will turn grey as any other control:
<GroupBox>
<GroupBox.Header>
<Label Content="My Header Text" Padding="0" />
</GroupBox.Header>
</GroupBox>
If you just assign a string to the Header
property, WPF will use a TextBlock
to visualize the header, but since TextBlock
is just a FrameworkElement
and not a Control
like Label
, it does not support being disabled (see Differences between Label and TextBlock).
It is necessary to define a zero padding (Padding="0"
) for the Label
, because the default padding is five, which kinda looks ugly as a header of a GroupBox
.
来源:https://stackoverflow.com/questions/20143803/disable-groupbox-including-the-groupbox-name-in-wpf