Search code examples
c#uwpfocustextblock

Focus error in TextBox with AppBarButton - UWP


I am using this code to make a button in XAML within a MVVM project in UWP.

for this use this code:

<CommandBar Background="Yellow" >
    <AppBarButton Label="Pruebas" Command="{Binding cmd_Process}" >
          <AppBarButton.Icon>
               <SymbolIcon Symbol="Setting"/>
          </AppBarButton.Icon>
     </AppBarButton>
</CommandBar>

At the moment of "clicking" the 'Command' the method that I need is executed in the ViewModel, but I have to send a content of a TextBlock to work with it in the ViewModel.

All good, but at the time of sending it sends me null, so that I do not send null I have to change the configuration of the Focus to the TextBlock, but I have to do it first, and then execute the Command.

If I use a normal button to link the Command works perfect, but not with the AppBarButton.

How do I make the AppBarButton change the focus of the TextBlock before executing the Command? Or how do I control the fucus from the ViewModel, knowing that I have to call the page from the ViewModel class?


Solution

  • You Can Lost the Focus in the TextBox with te property:

    UpdateSourceTrigger=PropertyChanged
    

    use into the TextBox... Example:

    <TextBox x:Name="txt_Name" Header="Name:" Margin="5" Text="{Binding PropertyBindingToViewModel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>