1

我已经为我的 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
4

2 回答 2

2

我希望触发器将可见性设置为父容器的“折叠”或“可见”,即 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 的东西。

于 2014-03-07T19:59:32.203 回答
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; }
    }
}
于 2014-03-07T19:25:39.183 回答