简体   繁体   English

列表框CurrentItem / SelectedItem

[英]ListBox CurrentItem / SelectedItem

I'm using a custom ListBoxItem, that's built like this 我正在使用一个自定义的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>

It's a basic structure which is modified for the editing control that I need, like this 这是一个基本结构,针对我需要的编辑控件进行了修改,例如

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

The used ListBoxItem for a certain record of the used ItemsSource of the ListBox is chosen by a StyleSelector. 由StyleSelector选择ListBox的ItemsItemSource的特定记录所使用的ListBoxItem。

What I need is a possibility to access the ListBoxItem, thats currently focused. 我需要的是访问ListBoxItem的可能性,这就是当前的重点。 I've tried the following 我尝试了以下

  • set the IsSyncronizedWithCurrentItem-property of the ListBox to true 将ListBox的IsSyncronizedWithCurrentItem-property设置为true
  • try to grab the SelectionChanged Event of the ListBox 尝试获取ListBox的SelectionChanged事件
  • monitor the IsSelected property of the ListBoxItem 监视ListBoxItem的IsSelected属性
  • define (Multi)Trigger in the basic style for the ListBoxItem 在ListBoxItem的基本样式中定义(Multi)Trigger
  • set an EventSetter 设置一个EventSetter

Can anyone help me, please? 有人可以帮我吗?

Thanks a lot in advance 提前谢谢

Alright, here are my attempts: 好吧,这是我的尝试:

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

In the style of the ListBox the IsSyncronizedWithCurrentItem property is set to true. 按照ListBox的样式,IsSyncronizedWithCurrentItem属性设置为true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM