简体   繁体   English

图片没有集中在点击上

[英]Image doesn't get focus on click

I have WPF application in which there are some controls on a screen layer. 我有WPF应用程序,其中的屏幕层上有一些控件。 Navigating between the controls using TAB (keyboard) works appropriately, and I can see the image gets focus using SNOOP. 使用TAB(键盘)在控件之间导航是正确的,我可以看到使用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. 单击Image元素时,默认情况下它不会聚焦。 You could write some code that focuses it though. 您可以编写一些重点代码。 Just handle the MouseLeftButtonDown event: 只需处理MouseLeftButtonDown事件:

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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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