Listview select "active" item

Possible duplicate:
Select ListBoxItem if TextBox in ItemTemplate gets focus

I have ListViewone related to ObservableCollection( Listview.ItemsSource). Listview provides several text fields that are bound to the properties of objects in the observed collection.

I would like to have the following functions: when the user focuses on the text field, the corresponding item in the list should be selected.

I tried things with ContainerFromElement, ContainerFromItem, etc., but I can not get this "simple" functionality to work.

Any ideas ...

+5
source share
2 answers

IsKeyboardFocusWithin ItemContainerStyle:

<ListView ItemsSource="{Binding}">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Style.Triggers>
                <Trigger Property="IsKeyboardFocusWithin" Value="True">
                    <Setter Property="IsSelected" Value="True" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Path=YourPropertyValue}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

, IsSelected true, .

: ; TextBox

Joep, , ( , , TextBox), , IsSelected reset false. , Style , , .

, , SelectionMode ListView Single; .

<ListView ItemsSource="{Binding}" SelectionMode="Single">
   <ListView.ItemContainerStyle>
       <Style TargetType="ListViewItem">
           <Style.Triggers>
               <Trigger Property="IsKeyboardFocusWithin" Value="True">
                   <Trigger.EnterActions>
                       <BeginStoryboard>
                           <Storyboard>
                               <BooleanAnimationUsingKeyFrames
                                  Storyboard.TargetProperty="IsSelected">
                                  <DiscreteBooleanKeyFrame KeyTime="0:0:0" 
                                     Value="True" />
                               </BooleanAnimationUsingKeyFrames>
                           </Storyboard>
                       </BeginStoryboard>
                   </Trigger.EnterActions>
               </Trigger>
           </Style.Triggers>
       </Style>
   </ListView.ItemContainerStyle>
   <!-- ... -->
</ListView>
+6

MVVM ViewModel, .

, ViewModel Name, IsNameFocussed, Address, IsAddressFocussed.

DataTemplate Is... Focussed, .

, , Is... Focussed GotFocus LostFocus . ( Focussed Property, ...)

0

All Articles