【问题标题】:How to make my button grid fit my hexagon image in WPF-XAML如何使我的按钮网格适合我在 WPF-XAML 中的六边形图像
【发布时间】:2026-01-08 09:15:02
【问题描述】:

我的按钮有一个六边形图像,如图所示。我想从角落边缘删除点击,并且只有在点击图片时才会被点击。我尝试了一些解决方案,因为我的可点击网格总是方形的,这就是为什么我的按钮点击区域比我的图片大。

<ControlTemplate TargetType="{x:Type Button}">
                    <Grid x:Name="LayoutGrid"
                          SnapsToDevicePixels="true">
                        <ContentPresenter x:Name="contentPresenter"
                                          Focusable="True"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          RecognizesAccessKey="True"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          Margin="0"
                                          IsHitTestVisible="True"
                                          ToolTip="Button">
                            <ContentPresenter.Content>
                                <Image Source="/WpfApp3;component/Images/hexagonImage.png"
                                       Stretch="None" />
                            </ContentPresenter.Content>
                        </ContentPresenter>
                        <Label Content="Label"
                               HorizontalAlignment="Center"
                               VerticalAlignment="Center"
                               Margin="60,130,60,51"
                               Width="180"
                               Height="40"
                               HorizontalContentAlignment="Center"
                               RenderTransformOrigin="0.428,-0.075"
                               FontSize="18"
                               FontFamily="Arial Narrow"
                               IsHitTestVisible="False" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsDefaulted"
                                 Value="true" />
                        <Trigger Property="IsMouseOver"
                                 Value="true" />
                        <Trigger Property="IsPressed"
                                 Value="true" />
                        <Trigger Property="IsEnabled"
                                 Value="false">
                            <Setter Property="TextElement.Foreground"
                                    TargetName="contentPresenter"
                                    Value="{StaticResource Button.Disabled.Foreground}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>

【问题讨论】:

  • 查看这个 (*.com/questions/17532063/…) 链接。这是关于使用 ControlTemplate 根据您的喜好塑造一个按钮,在您的情况下是一个六边形。
  • @ivica,尝试使用十六进制几何图形在 LayoutGrid 上设置 Clip 属性 (Clip)
  • 我找到了问题所在。即使背景边缘是透明的,它们仍然存在,这会产生问题。我已经制作了自定义控制(按钮),并且在里面我绘制了一条与我的图片完全相同的路径并且与图片居中相同,然后将我的路径设置为可点击和透明并且我的图片不可点击,这就解决了问题。

标签: c# wpf xaml


【解决方案1】:

这是我已经完成的答案,以防将来有人需要类似的东西。 1.rst 我已经从 Button 派生了 customControl。在里面我有 2 个 DependencyProperties,只是为了在点击时更改图像。

public ImageSource Image
        {
            get { return (ImageSource)GetValue(ImageProperty); }
            set { SetValue(ImageProperty, value); }
        }
       public static readonly DependencyProperty ImageProperty =
            DependencyProperty.Register("Image", typeof(ImageSource), typeof(CustomControl), 
                new FrameworkPropertyMetadata(default(ImageSource), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

        public ImageSource NewImage
        {
            get { return (ImageSource)GetValue(NewImageProperty); }
            set { SetValue(NewImageProperty, value); }
        }

        // Using a DependencyProperty as the backing store for NewImage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NewImageProperty =
            DependencyProperty.Register("NewImage", typeof(ImageSource), typeof(CustomControl),
                 new FrameworkPropertyMetadata(default(ImageSource), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

在 Generic.xaml 内部,我有这样的结构化代码(这适用于我和我的项目)。

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:wpf.core">


<Style TargetType="{x:Type local:CustomControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid HorizontalAlignment="Center"
                      VerticalAlignment="Center"
                      Width="{Binding ActualWidth, ElementName=image}"
                      Height="{Binding ActualHeight, ElementName=image}">

                    <Image x:Name="image"
                           IsHitTestVisible="False"
                           Stretch="None"
                           VerticalAlignment="Center"
                           HorizontalAlignment="Center">
                        <Image.Style>
                            <Style TargetType="{x:Type Image}">
                                <Setter Property="Source"
                                        Value="{Binding Image, RelativeSource={RelativeSource TemplatedParent}}" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding IsPressed, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"
                                                 Value="true">
                                        <Setter Property="Source"
                                                Value="{Binding NewImage, RelativeSource={RelativeSource TemplatedParent}}" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Image.Style>
                    </Image>
                    <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}"
                                      Content="{TemplateBinding Content}"
                                      ContentStringFormat="{TemplateBinding ContentStringFormat}"
                                      Height="48"
                                      IsHitTestVisible="False"
                                      Margin="38.667,0,38,46.666"
                                      VerticalAlignment="Bottom"
                                      HorizontalAlignment="Center"
                                      MaxHeight="48"
                                      MaxWidth="188" />
                    <Path Data="M2,88.60254L152,2 302,88.60254 302,261.817621 152,350.410161 2,261.807621z"
                          RenderTransformOrigin="0.5,0.5"
                          Stretch="Fill"
                          Fill="Transparent"
                          Focusable="True"
                          StrokeThickness="0"
                          Margin="25.333,-24.001,25.333,-23.999"
                          VerticalAlignment="Stretch"
                          Opacity="0">
                        <Path.RenderTransform>
                            <TransformGroup>
                                <ScaleTransform />
                                <SkewTransform />
                                <RotateTransform Angle="90" />
                                <TranslateTransform />
                            </TransformGroup>
                        </Path.RenderTransform>
                    </Path>
                </Grid>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed"
                             Value="true">
                        <Setter Property="Foreground"
                                Value="White" />
                    </Trigger>

                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

诀窍是制作与我的图片完全相同的路径(六边形),并将该路径设置为可点击但透明,图片可见但不可点击,这解决了我的问题,现在我功能齐全六角按钮。

主窗口中的代码示例,仅用于说明目的。

    <Grid>
    <cc:CustomControl Image="/WpfApp3;component/Images/hexPicture1.png"
                      NewImage="/WpfApp3;component/Images/hexPicture2.png"
                      ToolTip="Button"
                      Content="Some content"
                      FontFamily="Arial narrow"
                      FontSize="18"/>
</Grid>

【讨论】:

    最近更新 更多