wpf - 如何在启用或禁用按钮时更改自定义按钮中标签的前景色?

标签 wpf button triggers styles isenabled

给定以下带有自定义内容的 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>

最佳答案

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

关于wpf - 如何在启用或禁用按钮时更改自定义按钮中标签的前景色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23416650/

相关文章:

c# - 在post请求中发送文件+参数

wpf - XBAP 无法在 Mozilla Firefox 中运行

c - 有没有更好的方法来删除标题栏上的所有三个按钮并使用 Windows API 删除滚动条?

jquery - JavaScript/jQuery – 如何区分触发点击和用户点击?

c# - 翻转/镜像图像

c# - 在 C#/WPF 中从线程显示 MessageBox,就像它来自主线程一样

IOS 如何使用按钮发送消息?

HTML onclick 下拉菜单

postgresql - postgres - 在事务提交之前触发

sql-server - 审计 SQL Server 中的两个不同表