0

我有一个ListBox使用我的自定义ItemTemplate。我想根据所选项目Visibility在我的(在我的模板内)设置属性。TextBlock我想用触发器来做。但是如果当前项目被选中,我怎么知道在我的模板中?

<DataTemplate x:Key="myTemplate">
    <StackPanel Orientation="Horizontal">
        <Image Tag="{Binding priority}" Loaded="SetIconPriority"/>
        <Image Tag="{Binding alarm}" Loaded="SetIconAlarm"/>
        <!-- I want this TextBlock to be visible only when item is selected -->
        <TextBlock Text="{Binding description}"/>
    </StackPanel>
</DataTemplate>

编辑:它有效,谢谢!代码:

            <TextBlock Grid.Column="2" Grid.Row="1" Text="{Binding opis}">
                <TextBlock.Style>
                    <Style TargetType="TextBlock">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem}}" Value="False">
                                <Setter Property="Visibility" Value="Collapsed"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
4

1 回答 1

3

使用与beingRelativeSource绑定。AncestorTypeListBoxItem

<DataTrigger Binding="{Binding IsSelected,
                               RelativeSource={RelativeSource AncestorType=ListBoxItem}}"
             Value="True">

(可能想要反转逻辑并Collapse改为False,避免使用默认值Setter

于 2013-09-10T22:30:27.883 回答