ListBox CurrentItem / SelectedItem

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ListBox CurrentItem / SelectedItem相关的知识,希望对你有一定的参考价值。

我正在使用自定义的ListBoxItem,它是这样构建的

<Style x:Key="MyListBoxItem"  TargetType="{x:Type ListBoxItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border>
                    <Border>
                        <ContentPresenter />
                    </Border>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

它是一个基本结构,为我需要的编辑控件进行了修改,就像这样

<Style x:Key="MyListBoxItemText"  TargetType="{x:Type ListBoxItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <ListBoxItem Style="{DynamicResource MyListBoxItem}">
                    <TextBox  />
                </ListBoxItem>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

ListBox的已使用ItemsSource的特定记录的已使用ListBoxItem由StyleSelector选择。

我需要的是访问当前关注的ListBoxItem的可能性。我尝试了以下内容

  • 将ListBox的IsSyncronizedWithCurrentItem属性设置为true
  • 尝试抓住ListBox的SelectionChanged事件
  • 监视ListBoxItem的IsSelected属性
  • 在ListBoxItem的基本样式中定义(多个)触发器
  • 设置一个EventSetter

有人可以帮帮我吗?

非常感谢提前

答案

好的,这是我的尝试:

<ControlTemplate.Triggers>
    <DataTrigger  Binding="{Binding Selector.IsSelected}" Value="True">
        <Setter Property="Background" Value="Salmon" />
    </DataTrigger>
</ControlTemplate.Triggers>


<ControlTemplate.Triggers>
    <DataTrigger  Binding="{Binding ListBoxItem.IsSelected}" Value="True">
        <Setter Property="Background" Value="Salmon" />
    </DataTrigger>
</ControlTemplate.Triggers>


<ControlTemplate.Triggers>
    <DataTrigger  Binding="{Binding IsSelected}" Value="True">
        <Setter Property="Background" Value="Salmon" />
    </DataTrigger>
</ControlTemplate.Triggers>

在ListBox的样式中,IsSyncronizedWithCurrentItem属性设置为true。

以上是关于ListBox CurrentItem / SelectedItem的主要内容,如果未能解决你的问题,请参考以下文章