简体   繁体   中英

Image doesn't get focus on click

I have WPF application in which there are some controls on a screen layer. Navigating between the controls using TAB (keyboard) works appropriately, and I can see the image gets focus using SNOOP. BUT - Clicking the image doesn't set the focus on it.

If it matters - I enter a function I need via both (click and enter) events handlers... Just the focus is not being recieved in the click case, that's the confusion I can't understand.

Have you considered using a templated Button to show your image? You get the focus-on-click behavior, but show an image:

<Button>
    <Button.Template>
        <ControlTemplate TargetType="Button">
            <Image Source="http://placehold.it/300x500" Stretch="Fill" />
        </ControlTemplate>
    </Button.Template>
</Button>

An Image element doesn't get focused by default when you click on it. You could write some code that focuses it though. Just handle the MouseLeftButtonDown event:

private void img_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    Image img = sender as Image;
    img.Focusable = true;
    img.Focus();
}

<Image Source="pic.png" MouseLeftButtonDown="img_MouseLeftButtonDown" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM