gpt4 book ai didi

wpf - 如何将另一个 DependencyProperty 绑定(bind)到 CheckBox 的 IsChecked 属性?

转载 作者:行者123 更新时间:2023-12-03 13:43:30 25 4
gpt4 key购买 nike

这是我要完成的示例:

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

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



那么如何将 ListBox 上的属性绑定(bind)到 CheckBox 的 IsChecked 属性呢?

最佳答案

尝试使用 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>

关于wpf - 如何将另一个 DependencyProperty 绑定(bind)到 CheckBox 的 IsChecked 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2915986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com