0

私はWPFが初めてで、WPFの基本を学んでいます。私が欲しいのは、CheckBox がチェックされたときに Button の背景を緑にすることです。

以下は私が書いたコードです:

<Window x:Class="MyApplication.DataTrigger2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="DataTrigger2" Height="300" Width="300" Loaded="Window_Loaded">
    <Window.Resources>
        <Style x:Key="styleDataButton" TargetType="Button">
            <Style.Triggers>
                <DataTrigger Binding="{Binding chk}" Value="checked">
                    <Setter Property="Background" Value="Gold"/>
                </DataTrigger>
            </Style.Triggers>

        </Style>
    </Window.Resources>
    <Grid>
        <Button x:Name="btn" Height="50" Width="100" Content="Button" Margin="89,33,89,178" Style="{StaticResource styleDataButton}"/>
        <CheckBox x:Name="chk" Content="Checkbox" Height="50" Width="100" Margin="89,106,89,105"></CheckBox>
    </Grid>
</Window>
4

1 に答える 1

0

CheckBox要素にバインドする必要があります。Binding="{Binding chk}"DataContextの「chk」プロパティにバインドしていることを意味します。次のように変更する必要があります。

<DataTrigger Binding="{Binding ElementName=chk, Path=IsChecked}" Value="True">
     <Setter Property="Background" Value="Gold"/>
</DataTrigger>

IsCheckedそうすれば、あなたはあなたの財産に拘束されCheckBox、値が真であるかどうかをチェックしています。

于 2012-10-12T06:08:24.433 に答える