如何在WPF、按钮或边框/文本区域中创建圆角的可点击颜色区域?

如何在WPF、按钮或边框/文本区域中创建圆角的可点击颜色区域?,wpf,layout,Wpf,Layout,我想有四个彩色区域是可点击的。 我可以在边框中使用TextBlock来获取颜色区域,但它们都没有单击事件。 所以我可以把整个东西做成一个按钮,但是它没有圆角,而且我似乎不能改变背景 推荐的方法是什么,以下是我到目前为止得到的: <Window x:Class="WpfApplication6.Window7" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://s

我想有四个彩色区域是可点击的。 我可以在边框中使用TextBlock来获取颜色区域,但它们都没有单击事件。 所以我可以把整个东西做成一个按钮,但是它没有圆角,而且我似乎不能改变背景

推荐的方法是什么,以下是我到目前为止得到的:

<Window x:Class="WpfApplication6.Window7"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window7" Height="300" Width="300">
    <UniformGrid>
        <UniformGrid.Resources>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="FontSize" Value="20"/>
                <Setter Property="HorizontalAlignment" Value="Center"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
            </Style>
        </UniformGrid.Resources>
        <Button BorderThickness="1px" Margin="10" BorderBrush="Blue" CornerRadius="33" Background="Orange">testing1</Button>
        <Border BorderThickness="1px" Margin="10" BorderBrush="Blue" CornerRadius="10" Background="Yellow">
            <TextBlock>testing2</TextBlock>
        </Border>
        <Border BorderThickness="1px" Margin="10" BorderBrush="Blue" CornerRadius="10" Background="LightBlue">
            <TextBlock>testing3</TextBlock>
        </Border>
        <Border BorderThickness="1px" Margin="10" BorderBrush="Blue" CornerRadius="10" Background="LightGreen">
            <TextBlock>testing4</TextBlock>
        </Border>
    </UniformGrid>
</Window>

测试1
测试2
测试3
测试4

您必须为按钮定义自定义模板

例如:

<UniformGrid>
  <UniformGrid.Resources>
    <ControlTemplate x:Key="buttonTemp">
      <Border Margin="10" CornerRadius="10" Background="Yellow">
        <TextBlock Text="{TemplateBinding Button.Content}"/>
      </Border>
    </ControlTemplate>
  </UniformGrid.Resources>
  <Button Template="{StaticResource buttonTemp}">testing1</Button>
</UniformGrid>

测试1

另请参见

我相信您也可以这样做,尽管我还没有尝试过

<Border CornerRadius="5" ButtonBase.Click="ButtonClickHandler" />