2012-10-12 24 views
0

我是WPF的新手,學習WPF的基礎知識。我想要的是當一個CheckBox被選中,然後使Button的背景變成綠色。WPF中的數據查詢器

以下是代碼,我已經寫了:

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

回答

0

您需要綁定到CheckBox元素。 Binding="{Binding chk}"表示您綁定到DataContext的「chk」屬性。你應該將其更改爲:

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

這樣的話,你綁定到你的CheckBoxIsChecked財產和檢查,如果值是true。