ListBox CurrentItem / SelectedItem

694 views Asked by At

I'm using a custom ListBoxItem, that's built like this

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

What I need is a possibility to access the ListBoxItem, thats currently focused. I've tried the following

  • set the IsSyncronizedWithCurrentItem-property of the ListBox to true
  • try to grab the SelectionChanged Event of the ListBox
  • monitor the IsSelected property of the ListBoxItem
  • define (Multi)Trigger in the basic style for the ListBoxItem
  • set an EventSetter

Can anyone help me, please?

Thanks a lot in advance

1

There are 1 answers

0
Joe M. Doe On

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.