0

我正在使用一个自定义的 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 的基本样式中定义 (Multi)Trigger
  • 设置一个 EventSetter

任何人都可以帮助我吗?

非常感谢提前

4

1 回答 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。

于 2018-01-03T11:05:44.090 回答