如何在WPF,Button或Border / Textarea中制作带圆角的可点击颜色区域?

时间:2009-01-26 11:56:15

标签: wpf layout

我想要有四个可点击的彩色区域。 我可以在Border中使用TextBlock来获取颜色区域,但是它们都没有Click事件。 所以我可以把整个事情做成一个按钮,但它没有圆角,我似乎无法改变背景。

推荐的方法是什么,这是我到目前为止所做的:

<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>

2 个答案:

答案 0 :(得分:2)

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

例如:

<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>

另见http://mark-dot-net.blogspot.com/2007/07/creating-custom-wpf-button-template-in.html

答案 1 :(得分:-1)

我相信你也可以这样做,虽然我还没试过。

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