简体   繁体   English

如何将另一个DependencyProperty绑定到CheckBox的IsChecked属性?

[英]How can I bind another DependencyProperty to the IsChecked Property of a CheckBox?

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: 当我尝试运行它时,我得到了这个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. '绑定'只能在DependencyObject的DependencyProperty上设置。

So how can I bind a property on the ListBox to the IsChecked property of a CheckBox? 那么如何将ListBox上的属性绑定到CheckBox的IsChecked属性?

Try using a DataTrigger. 尝试使用DataTrigger。 Replace your Grid.Resources with the following and it works: 用以下内容替换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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 WPF 复选框的 IsChecked 属性绑定到非窗口对象的布尔属性 - How can I bind a WPF checkbox's IsChecked property to a boolean property of an object that is not a window 如何在ItemsControl中绑定CheckBox的IsChecked属性? - How do I bind IsChecked property of CheckBox within an ItemsControl? 如何将两个东西绑定到复选框的 IsChecked 属性? - How to bind two things to IsChecked property of checkbox? 如何将对象的布尔属性绑定到CheckBox的IsChecked属性? - How to bind an object's boolean property to a CheckBox's IsChecked property? 如何为我的bool属性正确实现INotifyPropertyChanged并绑定到CheckBox.IsChecked? - How do I correctly implement INotifyPropertyChanged for my bool property and bind to CheckBox.IsChecked? 在WPF中,如何将Checkbox的IsChecked绑定到List &lt;&gt;。Contains? - In WPF, how do I two-way bind a Checkbox's IsChecked to property to List<>.Contains? 将DependencyProperty绑定到另一个类中的Property - Bind DependencyProperty to Property in another class 如何将CheckBox IsChecked绑定到CheckBoxes的ListBox - How to bind CheckBox IsChecked to ListBox of CheckBoxes XAML:如何将DependencyProperty绑定到另一个对象上同名的C#属性? - XAML: How can I bind a DependencyProperty to a C# property of the same name on a different object? 将CheckBox的IsChecked绑定到方法 - Bind IsChecked of a CheckBox to a method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM