1

I knwow there are many other who wrote about this but the suggested solutions don't solve my problem.

I've got a sort of gallery and I need that when the user edits the number specified in the TextBox automatically the image is updated.

Initially I tried with LostFocus event, but it doesn't always fire as explained here.

If I bind the property as suggested here, I have too many events if the number has more than one digit.

Then I tried to do this, but it doesn't work.

This is how my TextBox is defined:

  <TextBox Margin="2" Text="{Binding NImageShown}" FontSize="18" 
           LostFocus="ImageIndex_OnLostFocus"
           Name="ImageIndex" Height="40" Width="60" 
           VerticalAlignment="Center" VerticalContentAlignment="Center" 
           Focusable="True">
  </TextBox>

I implemented the third solution by attaching the window to MouseDown event:

<Window x:Class="...."
    .....
     MouseDown="Window_MouseDown"....>

 private void Window_MouseDown(object sender, MouseButtonEventArgs e)
 {
      if (ImageIndex.IsFocused)
         Keyboard.ClearFocus();

  }

With this solution I sawy that ImageIndex.IsFocused remains true even after Keyboard.ClearFocus(): this is the reason why I think that solution does not fix my problem.

Any help is really appreciate!

EDIT Changes based on the suggestions:

<Grid Grid.Row="2" Grid.Column="2" Name="GridTextbox">
      <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
       </Grid.ColumnDefinitions>

       .....
       <StackPanel Grid.Column="2" Orientation="Horizontal"
              HorizontalAlignment="Right">
                <TextBox Margin="2" Text="{Binding NImageShown}" FontSize="18" LostFocus="ImageIndex_OnLostFocus"
                         Name="ImageIndex" Height="40" Width="60" VerticalAlignment="Center" VerticalContentAlignment="Center" Focusable="True"></TextBox>
                <Label Style="{DynamicResource LabelStyle}" VerticalAlignment="Center"
                       Content="{Binding NTotalImages}" FontSize="18" Padding="5,0,5,0" Margin="5,5,5,5" />
       </StackPanel>
 </Grid>

 private void Window_MouseDown(object sender, MouseButtonEventArgs e)
 {
     if (ImageIndex.IsFocused)
     {
         GridTextbox.Focus();
         Keyboard.ClearFocus();
     }
 }

Even with these changes, LostFocus doesn't fire.

SOLUTION

Change the xaml as suggested in this way and keep the code behind as written before:

 <TextBox Margin="2" Text="{Binding NImageShown}" FontSize="18" 
          LostKeyboardFocus="ImageIndex_OnLostFocus" 
          KeyDown="ImageIndex_OnLostFocus"
          Name="ImageIndex" Height="40" Width="60" 
          VerticalAlignment="Center" VerticalContentAlignment="Center" 
          Focusable="True">
  </TextBox>
ctt_it
  • 339
  • 1
  • 6
  • 22
  • When do you want to lose the focus? are you sure that window_mousedown is getting fired, and you are not setting the focus again in another sentence? – Ferus7 Jul 20 '17 at 08:01
  • True. The application will be used on a touchscreen, so I want to lose focus when the user click on the window and when the ENTER key is pressed. – ctt_it Jul 20 '17 at 08:06
  • Can you try to set the focus in the grid that contains your ImageIndex `Grid.Focus()` element and then `Keyboard.ClearFocus()`?, because maybe is a problem about scope – Ferus7 Jul 20 '17 at 08:11
  • https://stackoverflow.com/questions/38226094/clearfocus-on-a-textbox – Ferus7 Jul 20 '17 at 08:13
  • I tried, but it doesn't work. I'm going to edit my post so you can see the code. – ctt_it Jul 20 '17 at 08:38
  • have you tried LostKeyboardFocus instead of LostFocus event? https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/focus-overview – Arie Jul 20 '17 at 09:03

0 Answers0