0

I have a ListView that binds and presents my ObservableCollection fine with the exception of selecting the correct item in the ListView if I select a TextBox in one of the DataTemplate chosen UserControls. My DataTemplate selects a View based on the Type in the ObservableCollection, currently only of type TimeDelay:ModelBase or AddPoint:ModelBase.

If I select any area with the exception of a TextBox in the ListView of either ListTimeDelayView or ListAddPointView, the selection is fine. But, when a TextBox is selected, the ListView selection does not move, see image. The blue selection should move down to Move ddddd.

<UserControl.Resources>
    <DataTemplate DataType="{x:Type model:TimeDelay}">
        <local:ListTimeDelayView />
    </DataTemplate>

    <DataTemplate DataType="{x:Type model:AddPoint}">
        <local:ListAddPointView />
    </DataTemplate>
</UserControl.Resources>

ListView

<ListView ItemsSource="{Binding UserControlOneStatic.MotionSequenceCollection, Mode=TwoWay}"
     SelectedIndex="{Binding MotionSequenceStatic.MotionListViewSelected, Mode=TwoWay}"/>

Image below

enter image description here

FloppyDisk
  • 97
  • 2
  • 9
  • 1
    @Chris - You are correct. It was answered there and I used the following code: - – FloppyDisk Nov 04 '13 at 05:27
  • Good stuff, seemed like that might have been the same thing =D – Chris Nov 04 '13 at 08:52
  • I noticed that my bindings aren't updating:-( At least automatically until I click away. Which it wasn't doing beefore. So, I will look at it more closely when I get a second and will update. I went w/ the easy fix (short amount of code) vs the longer code & explanation on the other link. – FloppyDisk Nov 05 '13 at 13:36

1 Answers1

1

In WPF input events bubble up the element tree. You can handle GotKeyboardFocus on your ListView and get the original element and it's DataContext.

void myListView_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
    {
        var element = e.NewFocus as FrameworkElement;
        myListView.SelectedItem = element.DataContext;
    }

That's the basic idea. It should be the default behavior of ItemsControls, IMO.

EDIT: Chris linked to simpler and correct solution using XAML styling and triggers.

majocha
  • 1,161
  • 6
  • 12
  • thanks for the feedback. I feel bad as the question was already answered here. [Selecting a Textbox Item in a Listbox does not change the selected item of the listbox](http://stackoverflow.com/questions/653524/selecting-a-textbox-item-in-a-listbox-does-not-change-the-selected-item-of-the-l). As you said, basic idea is the same. – FloppyDisk Nov 04 '13 at 05:31