wpf WPF如何从数据模板设置父容器的可见性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22258217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF How to set visibility of parent container from a data template
提问by gmang
I have create a DataTemplate for my Component object. I added DataTrigger to determine if the component should be visible or not. Essentially we have filters and the code checks those filters to determine if the component should be Visible or Collapse. The issue I have is that I want the trigger to set the visibility to "Collapse" or "Visible" of the parent container, i.e a ListBoxItem. The code works but sets it at the Border instead.
我为我的 Component 对象创建了一个 DataTemplate。我添加了 DataTrigger 来确定组件是否应该可见。本质上我们有过滤器,代码检查这些过滤器以确定组件是否应该是可见的或折叠的。我遇到的问题是我希望触发器将可见性设置为父容器(即 ListBoxItem)的“折叠”或“可见”。该代码有效,但将其设置在 Border 上。
The template starts like this:
模板开始是这样的:
<DataTemplate DataType="{x:Type local:Component}">
<Border .....
I am providing the code for my Trigger and I'll explain what I tried below without success.
我正在为我的 Trigger 提供代码,我将解释我在下面尝试的内容但没有成功。
<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>
For the Setter Property
对于 Setter 属性
<Setter Property="Visibility" Value="Collapsed"></Setter>
I attempted to use the binding to get the listboxitem like this:
我尝试使用绑定来获取这样的列表框项:
<Setter Property="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}, Path=Visibility}" Value="Collapsed"></Setter>
I get this error when I try to run it, so I assume I can't use binding there at all and need a different approach?
当我尝试运行它时出现此错误,所以我认为我根本无法在那里使用绑定并且需要不同的方法?
A 'Binding' cannot be set on the 'Property' property of type 'Setter'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject
回答by paparazzo
Worked for me.
Don't know why it did not work for you?
对我来说有效。
不知道为什么它不适合你?
<ListBox x:Name="lb" ItemsSource="{Binding}" DisplayMemberPath="Text">
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="Visibility" Value="{Binding Path=Vis}" />
</Style>
</ListBox.Resources>
</ListBox>
public partial class MainWindow : Window
{
public MainWindow()
{
this.DataContext = this;
InitializeComponent();
List<TextVis> TextViss = new List<TextVis>();
TextVis tv1 = new TextVis();
tv1.Text = "tv1";
tv1.Vis = System.Windows.Visibility.Hidden;
TextViss.Add(tv1);
TextVis tv2 = new TextVis();
tv2.Text = "tv2";
tv2.Vis = System.Windows.Visibility.Visible;
TextViss.Add(tv2);
lb.ItemsSource = TextViss;
}
public class TextVis
{
public string Text { get; set; }
public Visibility Vis { get; set; }
}
}
回答by Federico Berasategui
I want the trigger to set the visibility to "Collapse" or "Visible" of the parent container, i.e a ListBoxItem
我希望触发器将可见性设置为父容器的“折叠”或“可见”,即 ListBoxItem
Change the ItemContainerStyle, like this:
改变ItemContainerStyle,像这样:
<ListBox ...>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.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>
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
BTW, you should really create a proper ViewModel and move all this logic to the ViewModel level instead of so much MultiBinding and Converter-based stuff.
顺便说一句,你真的应该创建一个合适的 ViewModel 并将所有这些逻辑移动到 ViewModel 级别,而不是那么多基于 MultiBinding 和 Converter 的东西。

