Forums

Event when ListBox selection is changed
Last Post 20 May 2017 04:43 AM by Matt Wilson. 8 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
J. Alan AthertonUser is Offline
New Member
New Member
Posts:45


--
06 Mar 2017 07:05 PM
    I would like to know when the selected item has changed for a listbox. I tried using the SelectionChanged event, but it appears to be unsupported. When I do it the way shown in the code below, RobotListSelectionChanged is never called. What is the best way to do this?

            <StackPanel Name="RobotPanel" Orientation="Vertical" Canvas.Left="120" Canvas.Top="0" MinWidth="500" Width="Auto" Background="{DynamicResource DefaultBackgroundColor}" Visibility="{Binding RobotPanelVisible}">
                <ListBox ItemsSource="{Binding RobotListItems}" SelectedItem="{Binding RobotListSelectedItem}" SelectionMode="Single">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Border Background="AliceBlue" Margin="5" HorizontalAlignment="Stretch" Width="Auto">
                                <Grid>
                                    <Grid.InputBindings>
                                        <MouseBinding MouseAction="LeftClick" Command="{Binding RobotListSelectionChanged}" />
                                    </Grid.InputBindings>
    
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>
    
                                    <TextBlock Width="200" Margin="5" Text="{Binding DisplayName}" Grid.Column="0" FontSize="12"/>
                                    <TextBlock Width="200" Margin="5" Text="{Binding IpAddress}" Grid.Column="1" FontSize="12"/>
    
                                </Grid>
                            </Border>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </StackPanel>
    
    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:674


    --
    06 Mar 2017 07:46 PM
    You could do it with Interactions, but easier and faster is to use property. So just put some code in RobotListSelectedItem property to detect change. That's what I do.
    J. Alan AthertonUser is Offline
    New Member
    New Member
    Posts:45


    --
    06 Mar 2017 07:57 PM
    Of course! I feel stupid. One of these days I'll get used to this framework Thanks.
    Matt WilsonUser is Offline
    New Member
    New Member
    Posts:7


    --
    28 Mar 2017 05:46 PM

    Posted By <a href='http://emptykeys.com/Activity-Feed/userid/1' class='af-profile-link'>Filip Dušek</a> on 06 Mar 2017 07:46 PM
    You could do it with Interactions, but easier and faster is to use property. So just put some code in RobotListSelectedItem property to detect change. That's what I do.


    I am curious about how to do it with an interaction too. My current implementation does what you suggest here, but I'd like to understand the alternative.

    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:674


    --
    29 Mar 2017 10:05 AM

    UI framework has Behaviors, which can be used to do magic. Implementation is based on Microsoft open source XamlBehaviors, so you can use similar tricks and hacks.

    
    <ListBox Grid.Row="1" Margin="2" ItemsSource="{Binding Tutorials}" HorizontalAlignment="Stretch" VerticalAlignment="Top" SelectedItem="{Binding SelectedTutorial}" TabIndex="10">
                <eki:Interaction.Behaviors>
                    <eki:EventTriggerBehavior EventName="SelectionChanged">
                        <eki:EventTriggerBehavior.Actions>
                            <eki:InvokeCommandAction Command="{Binding SelectionChangedCommand}" />
                        </eki:EventTriggerBehavior.Actions>
                    </eki:EventTriggerBehavior>
                </eki:Interaction.Behaviors>
    </ListBox>
    
    


    where eki is xmlns:eki="clr-namespace:EmptyKeys.UserInterface.Designer.Interactions;assembly=EmptyKeys.UserInterface.Designer" and SelectionChangedCommand is RelayCommand (or any ICommand).
    Matt WilsonUser is Offline
    New Member
    New Member
    Posts:7


    --
    29 Mar 2017 05:18 PM
    Awesome, thanks
    Matt WilsonUser is Offline
    New Member
    New Member
    Posts:7


    --
    18 May 2017 02:57 AM
    The behaviors work very well for what I have, but its making my XAML rather cluttered. Is it possible to make a staticresource or something similar that I can share between elements? I have this right now (the commands disable my pick ray so it doesn't do weird things when interacting with the UI with a mouse). I tried using a BehaviorCollection StaticResource, but the setter for Interaction.Behaviors is private.

    EDIT: I should note that this is the code for one UserControl in my project, there are a bunch more which is what drives this question. Lots of duplicate code
      <local:ProjectionDisplay DataContext="{Binding Projection}">
                    <eki:Interaction.Behaviors>
                        <eki:EventTriggerBehavior EventName="MouseEnter">
                            <eki:EventTriggerBehavior.Actions>
                                <eki:InvokeCommandAction Command="{Binding DisablePickRayCommand}" />
                            </eki:EventTriggerBehavior.Actions>
                        </eki:EventTriggerBehavior>
                        <eki:EventTriggerBehavior EventName="MouseLeave">
                            <eki:EventTriggerBehavior.Actions>
                                <eki:InvokeCommandAction Command="{Binding EnablePickRayCommand}" />
                            </eki:EventTriggerBehavior.Actions>
                        </eki:EventTriggerBehavior>
                    </eki:Interaction.Behaviors>
                </local:ProjectionDisplay>
     
    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:674


    --
    18 May 2017 07:33 PM
    You can't, that's one of the limitations of behaviors. But to be honest if you are using behaviors for detecting if your mouse is over UI, it's weird. You can just use InputManager.Current.MouseDevice.MouseOverElement.
    Matt WilsonUser is Offline
    New Member
    New Member
    Posts:7


    --
    20 May 2017 04:43 AM
    Oh hey, this is much cleaner. Thanks for the help! cut my XAML in half
    You are not authorized to post a reply.