0

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

Joe M. Doe
  • 61
  • 6
  • Can you show us how you tried using `Trigger`? I would use the `DataTrigger` and the `IsSelected` property. – XAMlMAX Jan 03 '18 at 08:16
  • Well, not really, sorry. The reason is that I was trying to bind/link to a EventHandler in the ViewModel, which didn't work at all. But is it possibl to see, what your solution would look like? – Joe M. Doe Jan 03 '18 at 10:43
  • How do you expect us to help without code examples? Surely you can skip _whatever_ sensitive information you want to keep private, it's about reproducing your attempt and then finding the right way. – XAMlMAX Jan 03 '18 at 10:48

1 Answers1

0

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.

Joe M. Doe
  • 61
  • 6