WPF如何从数据模板设置父容器的可见性

时间:2014-03-07 18:45:14

标签: wpf binding listboxitem multidatatrigger

我为Component对象创建了一个DataTemplate。我添加了DataTrigger以确定组件是否应该可见。基本上我们有过滤器,代码检查这些过滤器以确定组件是可见还是折叠。我遇到的问题是我希望触发器将可见性设置为父容器的“Collapse”或“Visible”,即ListBoxItem。代码可以工作,但是将其设置在边框处。

模板开头如下:

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

<Setter Property="Visibility" Value="Collapsed"></Setter>

我尝试使用绑定来获取listboxitem,如下所示:

 <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

2 个答案:

答案 0 :(得分:2)

为我工作。
不知道为什么它不适合你?

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

答案 1 :(得分:2)

  

我希望触发器将可见性设置为“Collapse”或“Visible”   父容器,即ListBoxItem

更改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>
顺便说一句,你应该真正创建一个合适的ViewModel并将所有这些逻辑移到ViewModel级别,而不是那么多基于MultiBinding和Converter的东西。