代码之家  ›  专栏  ›  技术社区  ›  Jesse Hallam

带Click事件的WPF图像控件

  •  35
  • Jesse Hallam  · 技术社区  · 14 年前

    我想用一个 <Image> 在响应鼠标单击的WPF应用程序中。只有“鼠标向上”和“Moue Down”事件是开箱即用的。我觉得这很特别。是否有扩展控件的方法或使用其他控件产生相同效果的方法?

    5 回复  |  直到 6 年前
        1
  •  67
  •   C8H10N4O2    14 年前

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

    创建这样的样式非常容易。只需在资源中使用命名键创建一个新的样式资源,并将此资源分配给按钮的样式属性。我举了一个例子:

    <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>
    
        2
  •  29
  •   Shawn Mclean    14 年前

    使用MouseUp。您不能通过任何方式切换或聚焦图像控件,因此这是单击它的唯一方法。

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

        3
  •  1
  •   Semm    10 年前

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

    布尔值:

    1. 被抛弃的
    2. I居中

    处理程序:

    1. OnMouseDown-集合被认为是真的;
    2. onmouseenter-设置为isentered true;
    3. onmouseleave-设置isentered false;
    4. onmouseup-if(isclicked&isentered){/ 你的逻辑 /}然后设为假;

    此结构实现了经典单击的功能。

        4
  •  0
  •   DonBoitnott    6 年前

    另一种选择是包装 Image 在一个 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>
    
        5
  •  -1
  •   Willem    9 年前
    1. onmouseleave-也设置为false; 否则,您可以单击鼠标并释放鼠标。然后单击鼠标并释放鼠标以继续单击。