【问题标题】:How do I change the Foreground color of a label in a custom button when the button is enabled or disabled?启用或禁用按钮时,如何更改自定义按钮中标签的前景色?
【发布时间】:2014-05-01 21:09:28
【问题描述】:

鉴于以下具有自定义内容的 WPF 按钮,我如何使用触发器将其标签的前景色在启用按钮时更改为蓝色,在禁用时更改为红色?

<Window x:Class="CustomButtonTriggerExample.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>
        <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
            <Button IsEnabled="{Binding ElementName=chkEnabled, Path=IsChecked}">
                <StackPanel Orientation="Horizontal">
                    <Label VerticalContentAlignment="Center">This is a label control inside a button</Label>
                    <Image Height="50" Width="50" Source="/CustomButtonTriggerExample;component/Images/Some Image.png" Stretch="Fill" />
                </StackPanel>
            </Button>
            <CheckBox x:Name="chkEnabled">Button Enabled</CheckBox>
        </StackPanel>
    </Grid>
</Window>

【问题讨论】:

    标签: wpf button triggers styles isenabled


    【解决方案1】:
    <Window x:Class="CustomButtonTriggerExample.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>
            <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
                <Button IsEnabled="{Binding ElementName=chkEnabled, Path=IsChecked}">
                    <Button.Resources>
                        <Style TargetType="Label">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}}, Path=IsEnabled}" Value="False">
                                    <Setter Property="Foreground" Value="Red" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}}, Path=IsEnabled}" Value="True">
                                    <Setter Property="Foreground" Value="Blue"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Resources>
    
                    <StackPanel Orientation="Horizontal">
                        <Label VerticalContentAlignment="Center">This is a label control inside a button</Label>
                        <Image Height="50" Width="50" Source="/CustomButtonTriggerExample;component/Images/Some Image.png" Stretch="Fill" />
                    </StackPanel>
                </Button>
                <CheckBox x:Name="chkEnabled">Button Enabled</CheckBox>
            </StackPanel>
        </Grid>
    </Window>
    

    【讨论】:

    • 这两个州都不需要DataTrigger。您可以使用SetterForeground 设置为蓝色,然后触发 Button.IsEnabed == false 并将其设置为红色。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多