1
votes

I'm trying to get my border focused after the user clicks on it. Currently it is possible to focus the border via tabs, but via click would be way more convenient for the user.

        <Border x:Name="BorderFileInfo" Focusable="True" BorderBrush="LightGray" BorderThickness="1">
            <Grid Margin="3,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left">
                <!-- CONTENT CTRL -->
            </Grid>
        </Border>

I saw in another post that there is a possability to catch the click event with an InputBinding but I don't know how to focus the border afterwards without using a command.

Stackoverflow: Why doesnt WPF border control have a mousedoubleclick event?

Is there an easy way to do that other than having to create commands ?

The app is pretty small so I don't want to use commands if I don't have to.

1

1 Answers

4
votes

One easy way is to handle PreviewMouseDown or similar mouse events and set the focus:

private void Border_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
     Keyboard.Focus(sender as Border);
}

edit

note that you can create Click by handling PreviewMouseLeftButtonDown and PreviewMouseLeftButtonUp in this way:

_isdown =false;
private void Border_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
     _isdown =true;
}
private void Border_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    if ( _isdown)
    {
         _isdown = false;
         Keyboard.Focus(sender as Border);
     }
}