0

我正在尝试制作一个播放器(某种)控件。我有一个按钮的用户控件已经可用(公司控件)。我试图给我的按钮我添加到其内容中的图像的形状。

问题是当我删除模板样式(这是为了根据图像边界适合我的按钮边界)时,当我尝试单击按钮时,我无法触发关联的 ToolBarCommand。本质上,我无法单击按钮本身。

凝视这并没有多大帮助,我试图对IsHitTestVisible="False"图像 - 但这也没有帮助。

以下是我的代码:

                <my:NewButton  Command="{Binding ToolBarCommand}" CommandParameter="Play">
                    <my:NewButton.Style>
                        <Style>
                            <Setter Property="my:NewButton.Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type my:NewButton}">
                                        <ContentPresenter HorizontalAlignment=" TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </my:NewButton.Style>
                    <my:NewButton.Content>
                        <Image Source="{Binding PlayState}" IsHitTestVisible="False" />
                    </my:NewButton.Content>
                </my:NewButton>

任何帮助深表感谢。PS:VS2012,.net 4.0

4

3 回答 3

1

您需要一些非空颜色并在按钮的可视树中命中测试可见像素,以使其具有命中测试可见性。

你故意设置IsHitTestVisible="False"在图像上。如果您删除它,它应该可以工作,并且如果图像具有透明像素 - 这些将不会“感觉到”点击。

如果您必须IsHitTestVisible="False"在图像上,则在控制模板中:

<Border Background="Transparent">
    <ContentPresenter HorizontalAlignment=" TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>

边框将是“感觉”鼠标点击的边框。

您甚至可以Padding在按钮上进行设置,并拥有比图像更大的命中测试区域。

于 2013-10-16T16:28:28.410 回答
0

ControlTemplate应用功能时,控件的外观应该完全改变。ContentPresent是将按钮功能继承到图像控制的简单方法之一。IsHitTestVisible=False使 Image 控件不触发 click 事件。你能试试下面的片段吗

<my:NewButton  Command="{Binding ToolBarCommand}" CommandParameter="Play">
   <my:NewButton.Template>
       <ControlTemplate TargetType="{x:Type my:NewButton}">
           <StackPanel>
               <Image Source="{Binding PlayState}"/>
               <ContentPresenter/>
           </StackPanel>
       </ControlTemplate>
   </my:NewButton.Template>
</my:NewButton>
于 2013-10-16T11:55:09.203 回答
0

如果您IsHitTestVisible="False"在图像上设置,则该按钮将不可点击,除非它“背后”有其他东西可以看到点击(如 XAMeLi 答案中的透明边框)。但是使用 IsHitTestVisible true (默认值),它应该是。在以下示例中,即使图像是透明的,也会触发 Click 事件:

<Button Click="Button_Click">
    <Button.Template>
        <ControlTemplate TargetType="Button">
            <ContentPresenter RecognizesAccessKey="True" />
        </ControlTemplate>
    </Button.Template>
    <Image Source="..." />
</Button>

因此,如果这对您不起作用,我认为您的NewButton课堂上一定发生了一些奇怪的事情。

如果我理解正确,您想让按钮的可点击区域由图像的非透明区域定义。

设置IsHitTestVisible使整个图像元素不可点击,而不仅仅是透明区域。这对您有帮助的唯一方法是,如果您在图像后面有一个透明的形状来捕捉点击。

至于使用图像本身,我认为 WPF 没有任何内置支持。您可以使用Window.AllowsTransparency以这种方式定义整个窗口的形状,但这对您没有帮助。

您将需要通过在要塑造的图像元素上覆盖HitTestCore来手动实施命中测试。这里有一个例子说明你可以如何做到这一点。

于 2013-10-16T18:25:36.807 回答