2010-05-26 6 views
3

가 여기에 내가 달성하기 위해 노력하고있어의 예 :다른 DependencyProperty를 CheckBox의 IsChecked 속성에 바인딩하려면 어떻게해야합니까?

<Window x:Class="CheckBoxBinding.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"> 

<StackPanel> 
    <CheckBox Name="myCheckBox">this</CheckBox>  
    <Grid> 
     <Grid.Resources> 
      <Style TargetType="ListBox"> 
       <Style.Triggers> 
        <Trigger Property="{Binding ElementName=myCheckBox, Path=IsChecked}" Value="True"> 
         <Setter Property="Background" Value="Red" /> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </Grid.Resources> 
     <ListBox> 
      <ListBoxItem>item</ListBoxItem> 
      <ListBoxItem>another</ListBoxItem> 
     </ListBox> 
    </Grid> 
</StackPanel> 
</Window> 

을 내가 그것을 실행하려고하면,이 XamlParseException 수 :

을 '속성을 설정할 수 없습니다'바인딩 ' '트리거 유형'의 '속성'. 'Binding'은 DependencyObject의 DependencyProperty에서만 설정할 수 있습니다.

그래서 ListBox의 속성을 CheckBox의 IsChecked 속성에 바인딩 할 수 있습니까?

답변

16

DataTrigger를 사용해보십시오. Grid.Resources를 다음으로 바꾸면 작동합니다 :

<Grid.Resources> 
     <Style TargetType="ListBox"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding ElementName=myCheckBox, Path=IsChecked}" Value="True"> 
        <Setter Property="Background" Value="Red" /> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Grid.Resources> 
+0

감사합니다! 그건 너무 의미가 :) –

관련 문제