DataTrigger无法在WPF ListBox中工作

时间:2017-03-10 15:53:57

标签: c# wpf xaml

我有以下WPF ListView,我根据我的班级 MyItemsClass 中的属性 ChangeView 更改模板。

<ListView x:Name="MyListView" ItemsSource="{Binding}">
    <ListView.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type myClass:MyItemsClass}}, Path=ChangeView}" Value="True">
                    <Setter Property="ListView.ItemsPanel" Value="{StaticResource largeTemplate}"/>
                </DataTrigger>

                <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type myClass:MyItemsClass}}, Path=ChangeView}" Value="False">
                    <Setter Property="ListView.ItemsPanel" Value="{StaticResource smallTemplate }" />
                </DataTrigger>
            </Style.Triggers>
         </Style>
     </ListView.Style>

<ItemsPanelTemplate x:Key="largeTemplate">
    <WrapPanel Orientation="Horizontal" IsItemsHost="True">
        <WrapPanel.Resources>
            <BooleanToVisibilityConverter x:Key="BoolToVisibility"/>
            <HierarchicalDataTemplate DataType="{x:Type local:MyData}">
                <Button Command="{Binding}" Visibility="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled, Converter={StaticResource BoolToVisibility}}">
                        <Image Height="100" Width="100"  Source="{Binding Path=MyImage}"/>
                </Button>
            </HierarchicalDataTemplate>
        </WrapPanel.Resources>
    </WrapPanel>
</ItemsPanelTemplate>

smallTemplate与largeTemplate XAML相同,仅具有不同大小的iamge。

现在我想使用现有的ListView样式'skin'我的ListView,所以如果我执行以下操作

<ListView x:Name="MyListView" ItemsSource="{Binding}" ItemContainerStyle="{DynamicResource MyStyle}">

</ListView>

<Style x:Key="MyStyle" TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource ListViewItemStyle}">
   <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListViewItem">
                <ContentPresenter />
            </ControlTemplate>
        </Setter.Value>
    </Setter> 
</Style>

所以现在我试图像这样添加原始触发器:

<ListView x:Name="MyListView" ItemsSource="{Binding}" ItemContainerStyle="{DynamicResource MyStyle}">

</ListView>

<Style x:Key="MyStyle" TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource ListViewItemStyle}">
   <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListViewItem">
                <ContentPresenter />
            </ControlTemplate>
        </Setter.Value>
    </Setter> 

    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type myClass:MyItemsClass}}, Path=ChangeView}" Value="True">
            <Setter Property="ListView.ItemsPanel" Value="{StaticResource largeTemplate}"/>
        </DataTrigger>

        <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type myClass:MyItemsClass}}, Path=ChangeView}" Value="False">
            <Setter Property="ListView.ItemsPanel" Value="{StaticResource smallTemplate }" />
        </DataTrigger>
    </Style.Triggers>
</Style>

但是现在我的DataTriggers不起作用 - 当我的类变量被更改时,ListView.ItemsPanel不会改变。我怎么能让这个工作?

2 个答案:

答案 0 :(得分:0)

请改为尝试:

rnn_out = tf.reshape(lstm_outputs, [-1, 100])
extra_params = tf.placeholder(shape=[10, 1], dtype=tf.float32)
fc_input = tf.concat([rnn_out, extra_params], 1)

我改变了你的样式以定位ListView本身的样式,而不是它的ItemContainerStyle属性,然后我将<ListView x:Name="MyListView" ItemsSource="{Binding}" Style="{DynamicResource MyStyle}"> </ListView> <Style x:Key="MyStyle" TargetType="{x:Type ListView}" BasedOn="{StaticResource ListViewStyle}"> <Setter Property="ItemTemplate"> <Setter.Value> <ControlTemplate TargetType="ListViewItem"> <ContentPresenter /> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type myClass:MyItemsClass}}, Path=ChangeView}" Value="True"> <Setter Property="ListView.ItemsPanel" Value="{StaticResource largeTemplate}"/> </DataTrigger> <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type myClass:MyItemsClass}}, Path=ChangeView}" Value="False"> <Setter Property="ListView.ItemsPanel" Value="{StaticResource smallTemplate }" /> </DataTrigger> </Style.Triggers> </Style> 更改为<Setter Property="Template">,这样它只会覆盖你的项目的模板,而不是然后覆盖整个<Setter Property="ItemTemplate">的模板。

答案 1 :(得分:0)

所以你有一个有效的ListView风格?为什么不简单地将它的ItemContainerStyle属性设置为“MyStyle”?:

<ListView x:Name="MyListView" ItemsSource="{Binding}" ItemContainerStyle="{DynamicResource MyStyle}">
    <ListView.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type myClass:MyItemsClass}}, Path=ChangeView}" Value="True">
                    <Setter Property="ListView.ItemsPanel" Value="{StaticResource largeTemplate}"/>
                </DataTrigger>

                <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type myClass:MyItemsClass}}, Path=ChangeView}" Value="False">
                    <Setter Property="ListView.ItemsPanel" Value="{StaticResource smallTemplate }" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListView.Style>
</ListView>

MyStyle适用于ListViewItem,而不适用于ListView,因此这应该有效。

或者您尝试申请的“皮肤”是什么?