2

カスタム コンテンツを含む次の 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>
4

1 に答える 1

6
<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>
于 2014-05-01T21:09:28.007 に答える