Forums

LostFocus and TextBox key handling
Last Post 30 Jun 2015 09:49 PM by J. Alan Atherton. 15 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


--
25 Jun 2015 10:43 PM
    Is there a way to handle the LostFocus event for a TextBox? Is there a list of supported events for controls? If not, can I find out what is supported by studying the UI Generator source code?

    Also, is there a way to capture keys (for example, space bar or enter) while typing in a textbox?
    For example: http://stackoverflow.com/questions/...-a-textbox

    Thanks!
    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:684


    --
    26 Jun 2015 09:04 AM
    If you don't see event in documentation (for example http://emptykeys.com/EmptyKeysUI/ht...ff4a96.htm ) then it's not supported. There is error/warning generated by UI Generator in some cases (if I can detect that). Lost Focus is event on UIElement class so it's supported.

    You can capture keys same way like in WPF

        <TextBox Name="searchInput" Text="{Binding Path=SearchText}" Grid.Column="2">
            <TextBox.InputBindings>
                <KeyBinding Key="Enter" Command="{Binding SearchCommand}" />
             </TextBox.InputBindings>
         </TextBox>
    
    J. Alan AthertonUser is Offline
    New Member
    New Member
    Posts:45


    --
    26 Jun 2015 04:19 PM
    Ok I will try that.

    This is a related question. Does the binding for TextBox support different UpdateSourceTrigger types? The code below still updates the binding as if it was PropertyChanged.

    <TextBox Width="200" Margin="5" Text="{Binding PathName, UpdateSourceTrigger=LostFocus}" Grid.Column="4" FontSize="12"/>
    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:684


    --
    26 Jun 2015 04:29 PM
    You are correct. UI supports only property changed mode. I felt like other modes are not that interesting. Do you really need LostFocus update mode for some scenario?
    J. Alan AthertonUser is Offline
    New Member
    New Member
    Posts:45


    --
    26 Jun 2015 05:08 PM
    What I need is to have some code handle the LostFocus event. So based on what you've said so far, it does not seem necessary to have other update modes. I am still trying to figure it out

    What goes in this block here if I want to execute code in the viewmodel? I know how to do button Command bindings, etc., but I don't know what tag type to put inside the EventTrigger.

    <EventTrigger RoutedEvent="TextBox.LostFocus">
    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:684


    --
    27 Jun 2015 12:25 AM
    I see. You can't do it in XAML at this moment. Even standard WPF have problem with that. You are right that it could be handy and implementation is not that hard.
    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:684


    --
    29 Jun 2015 10:42 AM
    You can do this in latest version

    <EventTrigger RoutedEvent="Button.MouseEnter" ek:EventTrigger.CommandPath="OnMouseEnterCommand" ek:EventTrigger.CommandParameter="test"> 


    CommandPath is name of the command property in your view model class. This feature is experimental for now. I might change it in future.
    J. Alan AthertonUser is Offline
    New Member
    New Member
    Posts:45


    --
    29 Jun 2015 08:20 PM
    Excellent. This is perfect. Thank you. I was reading about InvokeCommandAction for WPF, but it looks like there are limitations even to that, like you were saying. I will test this out and see how it works for us.
    J. Alan AthertonUser is Offline
    New Member
    New Member
    Posts:45


    --
    29 Jun 2015 11:59 PM
    I am having trouble getting this to work with a DataTemplate. I got it to work fine with normal, top-level, UI Elements. Even that is very useful, thank you for adding it.

    In the code below, it's the MouseEnter event triggers that I'm testing with. I have an ICommand property that is part of the class used for each listbox item.

    Is there something I'm missing, or is this a limitation?

    Here's some xaml:
                <ListBox ItemsSource="{Binding PathListItems}" MaxHeight="300">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Border CornerRadius="6" Background="AliceBlue" Margin="5" HorizontalAlignment="Stretch" Width="Auto">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition MinWidth="200" Width="Auto"/>
                                        <ColumnDefinition MinWidth="100" Width="Auto"/>
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>
    
                                    <ToggleButton Content="Visible" Margin="5" Grid.Column="0" FontSize="12" IsChecked="{Binding PathVisible}" Command="{Binding TogglePathVisibleCommand}" CommandParameter="{Binding PathName}" />
                                    <Button Content="Send" Margin="5" Grid.Column="1" FontSize="12" Command="{Binding SendPathCommand}" CommandParameter="{Binding PathName}"/>
                                    <ToggleButton Content="Set Default" Margin="5" Grid.Column="2" FontSize="12" IsChecked="{Binding PathDefault}" Command="{Binding TogglePathDefaultCommand}" CommandParameter="{Binding PathName}" />
                                    <Button Content="Edit" Margin="5" Grid.Column="3" FontSize="12" Command="{Binding EditPathCommand}" CommandParameter="{Binding PathName}">
                                        <Button.Triggers>
                                            <EventTrigger RoutedEvent="Button.MouseEnter" ek:EventTrigger.CommandPath="LostFocusCommand" ek:EventTrigger.CommandParameter="Nested Button"/>
                                        </Button.Triggers>
    
                                    </Button>
                                    <TextBox Width="200" Margin="5" Text="{Binding PathName}" Grid.Column="4" FontSize="12">
                                        <TextBox.Triggers>
                                            <EventTrigger RoutedEvent="TextBox.MouseEnter" ek:EventTrigger.CommandPath="LostFocusCommand" ek:EventTrigger.CommandParameter="Test"/>
                                        </TextBox.Triggers>
                                    </TextBox>
                                    <ComboBox Width="100" Margin="5" ItemsSource="{Binding ConstellationNames}" Grid.Column="5" FontSize="12" SelectedIndex="{Binding SelectedConstellation}"/>
                                    <Button Margin="5" Grid.Column="6" Command="{Binding DeletePathCommand}" CommandParameter="{Binding PathName}" FontSize="12" >
                                        <Image Width="30" Height="30" Source="{DynamicResource trashIcon}"/>
                                    </Button>
                                </Grid>
                            </Border>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
    
    
    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:684


    --
    30 Jun 2015 10:46 AM
    Hmm ... I will check that out
    J. Alan AthertonUser is Offline
    New Member
    New Member
    Posts:45


    --
    30 Jun 2015 04:56 PM
    Hold on, I just noticed a bug in my code. It appears to be working. Sorry for the misinformation.
    J. Alan AthertonUser is Offline
    New Member
    New Member
    Posts:45


    --
    30 Jun 2015 05:13 PM
    I just tried using a binding for the parameter and the object passed in was null. Is that expected for this experiment? Should the binding work, but maybe the path/scope needs to be different? Is there any other way to pass in a variable through CommandParameter?

    <TextBox Width="200" Margin="5" Text="{Binding PathName}" Grid.Column="4" FontSize="12">
                                        <TextBox.Triggers>
                                            <EventTrigger RoutedEvent="TextBox.MouseEnter" ek:EventTrigger.CommandPath="LostFocusCommand" ek:EventTrigger.CommandParameter="{Binding PathName}"/>
                                        </TextBox.Triggers>
                                    </TextBox>
    
    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:684


    --
    30 Jun 2015 05:26 PM
    yeah, didn't have time to do binding support for it in UI Generator. So it's not supported.
    J. Alan AthertonUser is Offline
    New Member
    New Member
    Posts:45


    --
    30 Jun 2015 08:11 PM
    What? You didn't have time? I asked you about this on Friday, you had all weekend before your Monday release!

    </sarcasm>

    Seriously, you are awesome for putting this in with such short time anyway, and I don't mean to sidetrack you from work that is higher priority to you.

    Now you say that it's a UI Generator thing that needs work. Because the source is available, is that something I could potentially patch in? Or is there some other work that needs to happen on the closed code?
    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:684


    --
    30 Jun 2015 08:46 PM
    Problem is that UI Generator does not support Binding for Attached properties. This is not big issue, because it's not used that much, but in this case, it would be handy.

    There could be some additional issues with binding on Event Trigger itself tho. I never get to that point.
    J. Alan AthertonUser is Offline
    New Member
    New Member
    Posts:45


    --
    30 Jun 2015 09:49 PM
    I see. Unfortunately my boss just told me that we've had a shift in priorities and I won't be able to spend much time for the next few weeks working on the UI.
    You are not authorized to post a reply.