wpf - WPF 模板化按钮 - 无点击事件
问题描述
我正在尝试设计一个周围有虚线边框和中间有图像的按钮。理想的命中测试将在图像本身上进行,但不是必需的。我的问题是我无法触发点击事件。这可能是一个命中测试问题,但我无法弄清楚。
除了按钮上明显的 onClick 之外,我还尝试了图像上的 MouseEvents、Stackpanel 上的 ButtonBase.Click、边框上的 Border.InputBindings .. 我认为设置 Background=Transparent 会做到这一点,但没有成功。
这是xml:
<Button Grid.Row="0" Click="Button_Click_1">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="Transparent" Grid.Row="0" Margin="30,50" CornerRadius="10" BorderThickness="4">
<Border.BorderBrush>
<VisualBrush>
<VisualBrush.Visual>
<Rectangle StrokeDashArray="4 2"
Stroke="#CEAC2D"
StrokeThickness="4"
RadiusX="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=CornerRadius.TopRight}"
RadiusY="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=CornerRadius.BottomLeft}"
Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=ActualWidth}"
Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=ActualHeight}"/>
</VisualBrush.Visual>
</VisualBrush>
</Border.BorderBrush>
<StackPanel Orientation="Vertical" VerticalAlignment="Center" ButtonBase.Click="Button_Click_1">
<Button Height="30" Width="30" Content="Start"/> // just a test button to see if it works
<Image Source="/Images/uploadIcon.png" Height="180"/>
<TextBlock Margin="0,20,0,0" Foreground="#CEAC2D" Text="Drag Folder or Click to Browse" FontFamily="Lato" FontSize="27"
FontWeight="SemiBold" HorizontalAlignment="Center"/>
</StackPanel>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
解决方案
您为什么要尝试将整个内容设置为按钮的模板?如果您只希望图像位实际可点击,则将其设置为按钮的控件模板并将其放置在常规可视树中:
<Border Background="Transparent" Grid.Row="0" Margin="30,50" CornerRadius="10" BorderThickness="4">
<Border.BorderBrush>
<VisualBrush>
<VisualBrush.Visual>
<Rectangle StrokeDashArray="4 2"
Stroke="#CEAC2D"
StrokeThickness="4"
RadiusX="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=CornerRadius.TopRight}"
RadiusY="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=CornerRadius.BottomLeft}"
Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=ActualWidth}"
Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=ActualHeight}"/>
</VisualBrush.Visual>
</VisualBrush>
</Border.BorderBrush>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<Button Click="Button_Click_1" Cursor="Hand">
<Button.Template>
<ControlTemplate TargetType="Button">
<Image Source="/Images/uploadIcon.png" Height="180"/>
</ControlTemplate>
</Button.Template>
</Button>
<TextBlock Margin="0,20,0,0" Foreground="#CEAC2D" Text="Drag Folder or Click to Browse" FontFamily="Lato" FontSize="27"
FontWeight="SemiBold" HorizontalAlignment="Center"/>
</StackPanel>
</Border>
推荐阅读
- reactjs - 如何在测试中获取反应应用状态 ['Key'] 的值?
- ruby-on-rails - 如何在 Rails 4 中设置嵌套资源控制器?
- javascript - 如何让我的提交按钮在不离开当前页面的情况下显示上传的文件
- android - Gradle 同步失败:读取超时
- python - Python将复杂的字符串转换为字典列表
- android - 如何在 Kotlin 中关闭 Android 应用程序
- reactjs - 在 Redux 中在哪里存储类实例以实现可重用性?
- angular - 使用谷歌表格填充角度表
- node.js - 节点和 Sqlite:返回有许多结果作为一行
- css - CSS Grid 不会填满整个视口