ToggleButton风格;覆盖IsEnabled



一旦ToggleButton被选中,按钮应该被设置为禁用,前景色应该改变。

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

我该怎么做?

禁用控件的外观在不同版本的windows和不同的windows帐户设置等中是不同的,这就是为什么它适用于其他人而不是你。一般来说,你应该尊重这些设置,但如果你真的很固执,那么你需要显式地覆盖模板,就像这个问题所示:

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

最新更新