ToggleButton风格;覆盖IsEnabled

时间:2015-09-01 18:04:31

标签: wpf

选中ToggleButton后,应将Button设置为disabled并且前景色应该更改。以下是行不通的:

      <Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ToggleButton  Content="Hallo" Margin="113,97,72,46" >
        <ToggleButton.Style>
            <Style TargetType="{x:Type ToggleButton}">
                <Setter Property="Foreground" Value="Blue"/>
                <Setter Property="IsEnabled" Value="True"/>
                <Setter Property="IsChecked" Value="False"/>
                <Style.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter Property="IsEnabled" Value="False"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Foreground" Value="Green"/>
                    </Trigger>

                </Style.Triggers>
            </Style>
        </ToggleButton.Style>
    </ToggleButton>

</Grid>

我该怎么做?

1 个答案:

答案 0 :(得分:0)

禁用控件的外观在不同版本的Windows和不同的Windows帐户设置等方面有所不同,这就是为什么它为其他人而不是你工作的原因。一般来说,您应该尊重这些设置,但如果您真的坚持,那么您需要明确覆盖模板,如this question中所示:

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type ToggleButton}">
            <Grid Background="{TemplateBinding Background}">
                <ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}"
                        HorizontalAlignment="Center" 
                        VerticalAlignment="Center" />
            </Grid>
        </ControlTemplate>
    </Setter.Value>
</Setter>