用于ListBox项的DataTemplate中的IsSelected的WPF触发器

时间:2011-06-13 18:58:24

标签: wpf templates listbox datatrigger

我有一个ListBox,其中设置了一系列DataTemplates,可根据数据类型正确显示内容。我正在尝试触发每个DataTemplate的外观,以便在选择ListBoxItem时从“只读”状态更改为“可编辑”状态。我的目标是使用“IsSelected”触发器设置它,并相应地显示/隐藏控件。

我以下列方式定义我的DataTemplates:

    <DataTemplate DataType="{x:Type vm:HeaderSlugViewModel}">
        <vw:HeaderSlugView />
    </DataTemplate>

    <DataTemplate DataType="{x:Type vm:ContentSlugViewModel}">
        <vw:ContentSlugView />
    </DataTemplate>

    <DataTemplate DataType="{x:Type vm:ImageSlugViewModel}">
        <vw:ImageSlugView />
    </DataTemplate>

每个“视图”都是一个独立的XAML文件。我希望能够在这些文件中设置触发器,查看ListBoxItem的IsSelected属性,以便控制其中各种控件的可见性。

覆盖ListBoxItem的模板如下:

    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Border Name="SlugContainer" Background="Transparent" BorderBrush="Black" BorderThickness="1" CornerRadius="2" Margin="0,5,0,0" Padding="5">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>

                            <!-- snipped for brevity -->

                            <ContentPresenter Grid.Row="1" />

                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我以下列方式修改了ContentPresenter,以便使用“FindAncestor”RelativeSource进行测试:

<ContentPresenter Grid.Row="1">
    <ContentPresenter.Style>
        <Style TargetType="{x:Type ContentPresenter}">
            <Setter Property="Visibility" Value="Collapsed" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="False">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ContentPresenter.Style>
</ContentPresenter>

这样可行,但是当我将类似的代码移动到代表View的XAML文件时,它不再看到触发器。例如:

<UserControl ...>
    <UserControl.Resources>
        <local:FlowDocumentToXamlConverter x:Key="flowDocumentConverter" />
    </UserControl.Resources>

    <UserControl.Style>
        <Style TargetType="{x:Type UserControl}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}">
                    <Setter Property="Visibility" Value="Hidden" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Style>

    <DockPanel Name="SlugContainer">
        <Label DockPanel.Dock="Top" Content="Filler" />
        <ctrl:BindableRichTextBox x:Name="TextBox" Document="{Binding Content, Converter={StaticResource flowDocumentConverter}, Mode=TwoWay}" LostFocus="OnLostFocus" />
    </DockPanel>
</UserControl>

如何选择ListBoxItem,最好是从View的XAML文件中检测?

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

你错过了:

Value =“True”

在你的视图中 - 可能是那个吗?