带有单击事件的 WPF 图像控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4151380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:17:42  来源:igfitidea点击:

WPF Image Control with Click Event

wpfxaml

提问by Jesse Hallam

I wish to use an <Image>in my WPF Application which responds to mouse clicks. Only the 'Mouse Up' and 'Moue Down' events are available out of the box. I find this rather particular. Is there a way to extend the control or a way to use another control to give the same effect?

我希望<Image>在我的 WPF 应用程序中使用响应鼠标点击的 。只有 'Mouse Up' 和 'Moue Down' 事件是开箱即用的。我觉得这很特别。有没有办法扩展控件或使用另一个控件来产生相同的效果?

回答by C8H10N4O2

To expand on Shawn Mclean's answer, one of the great capabilities of WPF is the ability to leverage the behavior of a control while completely changing the look of that control. If you want to create an image that behavior just like a button (complete with click events, command binding, default button assignment, etc.) you can place an image in a button control and restyle that button to remove the button "chrome". This will give you the nice API of a button with the look you desire. You can reuse this style and this approach will reduce the need to create event handlers in your code behind if you have commands to bind to your new image buttons.

为了扩展 Shawn Mclean 的回答,WPF 的一项强大功能是能够利用控件的行为,同时完全改变该控件的外观。如果您想创建一个行为就像按钮一样的图像(完成单击事件、命令绑定、默认按钮分配等),您可以将图像放在按钮控件中并重新设置该按钮的样式以删除按钮“chrome”。这将为您提供具有您想要的外观的按钮的漂亮 API。你可以重用这种风格,如果你有命令绑定到新的图像按钮,这种方法将减少在你的代码中创建事件处理程序的需要。

Creating such a style is pretty easy. Simply create a new style resource with a named key in a resource and assign this resource to the Style property of your buttons. Here is an example I threw together:

创建这样的样式非常容易。只需在资源中创建一个具有命名键的新样式资源,并将此资源分配给按钮的 Style 属性。这是我拼凑的一个例子:

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ButtonStyleTestApp.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">

<Window.Resources>
    <Style x:Key="NoChromeButton" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid x:Name="Chrome" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                    <ControlTemplate.Triggers>                          
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="#ADADAD"/>
                            <Setter Property="Opacity" TargetName="Chrome" Value="0.5"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<Grid x:Name="LayoutRoot" Background="#FF44494D">
    <Button Style="{DynamicResource NoChromeButton}" Click="ButtonClicked" >
        <Image Source="Desert.png" Stretch="None"/>
    </Button>
</Grid>
</Window>

回答by Shawn Mclean

Use MouseUp. You cannot tab through or focus on the Image control anyways so thats the only way to click it.

使用鼠标向上。无论如何,您不能通过 Tab 键或聚焦在 Image 控件上,因此这是单击它的唯一方法。

Another option is to just put the image in a button and remove all styles by editing the control template so it seems like just an image. That way, you can focus on it using the keyboard.

另一种选择是将图像放在按钮中并通过编辑控件模板删除所有样式,使其看起来只是一个图像。这样,您就可以使用键盘专注于它。

回答by Semm

You can use a set of 4 handlers and 2 boolean variables:

您可以使用一组 4 个处理程序和 2 个布尔变量:

booleans:

布尔值:

  1. IsClicked
  2. IsEntered
  1. 被点击
  2. 已输入

handlers:

处理程序:

  1. OnMouseDown- sets IsClicked true;
  2. OnMouseEnter- sets IsEntered true;
  3. OnMouseLeave - sets IsEntered false;
  4. OnMouseUp - if (IsClicked && IsEntered){ /TODO your logic/} and then sets IsClicked false;
  1. OnMouseDown- 设置 IsClicked 为真;
  2. OnMouseEnterset IsEntered true;
  3. OnMouseLeave - 设置 IsEntered false;
  4. OnMouseUp - if (IsClicked && IsEntered){ / TODO 你的逻辑/} 然后设置 IsClicked false;

This construction implements the functionality of the classic click.

这种结构实现了经典点击的功能。

回答by DonBoitnott

Another option is to wrap an Imagein a Hyperlink, like this:

另一种选择是将 an 包裹Image在 a 中Hyperlink,如下所示:

 <TextBlock>
    <Hyperlink Command="{Binding VisitWebsiteCommand}"
               CommandParameter="http://www.google.com"
               Cursor="Hand"
               TextDecorations="{x:Null}">
        <Image Height="50"
               Source="{StaticResource YourImageResource}"
               Width="50" />
    </Hyperlink>
</TextBlock>

回答by Willem

  1. OnMouseLeave - also set is isClicked to false; Otherwise you can click mouse away and release mouse. Then click mouse over and release mouse to still make a click happen.
  1. OnMouseLeave - 也设置 isClicked 为 false;否则,您可以单击鼠标并释放鼠标。然后单击鼠标并释放鼠标仍然进行单击。