views:

121

answers:

1

Here's an example of what I'm trying to accomplish:

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

When I try to run it, I get this XamlParseException:

A 'Binding' cannot be set on the 'Property' property of type 'Trigger'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

So how can I bind a property on the ListBox to the IsChecked property of a CheckBox?

+1  A: 

Try using a DataTrigger. Replace your Grid.Resources with the following and it works:

    <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>
Ben Collier
thanks! that makes sense too :)
speedmetal