ListBox CurrentItem / SelectedItem

问题描述 投票:0回答:1

我正在使用自定义的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

有人可以帮帮我吗?

非常感谢提前

wpf styles selecteditem listboxitem currentitem
1个回答
0
投票

好的,这是我的尝试:

<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。

© www.soinside.com 2019 - 2024. All rights reserved.