我已经为我的 Component 对象创建了一个 DataTemplate。我添加了 DataTrigger 以确定组件是否应该可见。本质上,我们有过滤器,代码检查这些过滤器以确定组件应该是可见的还是折叠的。我遇到的问题是我希望触发器将可见性设置为父容器的“折叠”或“可见”,即 ListBoxItem。该代码有效,但将其设置在 Border 处。
模板开始是这样的:
<DataTemplate DataType="{x:Type local:Component}">
<Border .....
我正在为我的触发器提供代码,我将在下面解释我尝试过的内容但没有成功。
<DataTemplate.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource TrueWhenComponentIsVisible}">
<Binding Path="Type" />
<Binding Path="Dependency"/>
<Binding Path="SelectedType" RelativeSource="{RelativeSource FindAncestor, AncestorType=Window}"/>
<Binding Path="SelectedDepencency" RelativeSource="{RelativeSource FindAncestor, AncestorType=Window}"/>
</MultiBinding>
</DataTrigger.Binding>
<DataTrigger.Setters>
<Setter Property="Visibility" Value="Visible"></Setter>
</DataTrigger.Setters>
</DataTrigger>
<DataTrigger Value="False">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource TrueWhenComponentIsVisible}">
<Binding Path="Type" />
<Binding Path="Dependency"/>
<Binding Path="SelectedType" RelativeSource="{RelativeSource FindAncestor, AncestorType=Window}"/>
<Binding Path="SelectedDepencency" RelativeSource="{RelativeSource FindAncestor, AncestorType=Window}"/>
</MultiBinding>
</DataTrigger.Binding>
<DataTrigger.Setters>
<Setter Property="Visibility" Value="Collapsed"></Setter>
</DataTrigger.Setters>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
对于 Setter 属性
<Setter Property="Visibility" Value="Collapsed"></Setter>
我尝试使用绑定来获取列表框,如下所示:
<Setter Property="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}, Path=Visibility}" Value="Collapsed"></Setter>
当我尝试运行它时出现此错误,所以我假设我根本不能在那里使用绑定并且需要不同的方法?
A 'Binding' cannot be set on the 'Property' property of type 'Setter'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject