我可能已经设法创建了您正在寻找的东西。请注意,这需要进一步的工作,因为很多功能都没有实现。我所做的只是创建一个“双按钮”,它具有单独的可点击部分,并且看起来与您提供的一样。
您可以根据我的示例制定自己的样式和功能。
首先,它的外观:
现在,进入代码。我将它定义为一个 userControl,以便于管理和重用,而不会使 XAML 过于繁琐。
XAML:
<UserControl x:Class="WpfApplication2.DualButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Button Click="Button_Click_1">
<Button.Template>
<ControlTemplate>
<Grid>
<Path Fill="Lime" >
<Path.Data>
<PathGeometry>
<PathFigure StartPoint="150,0">
<ArcSegment IsLargeArc="True"
Size="50, 50"
Point="150, 300"
SweepDirection="Counterclockwise" />
</PathFigure>
</PathGeometry>
</Path.Data>
</Path>
<TextBlock Margin="49,46,171,83" FontSize="150" TextAlignment="Center" HorizontalAlignment="Center" Foreground="White">S</TextBlock>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
<Button Click="Button_Click_2">
<Button.Template>
<ControlTemplate>
<Grid>
<Path Fill="Blue" >
<Path.Data>
<PathGeometry>
<PathFigure StartPoint="150,0">
<ArcSegment IsLargeArc="True"
Size="50, 50"
Point="150, 300"
SweepDirection="Clockwise" />
</PathFigure>
</PathGeometry>
</Path.Data>
</Path>
<TextBlock Margin="162,47,41,84" FontSize="150" TextAlignment="Center" HorizontalAlignment="Center" Foreground="White" Width="97">A</TextBlock>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
那么,这里到底发生了什么?
首先,当然是布局网格。它包含两个buttons,它们的模板设置为PathGeometry 以创建一个椭圆。 (这里已经有一些需要改进的地方——我使用了固定值,所以button 不会真正可扩展。尝试将这些属性与grid 大小连接起来)
然后有一个textBlock 用于按钮半边的明显字母。再一次,我并没有太在意放置,我只是把它们拖到周围,这样它们看起来不错。你也可以自己解决这个问题(我希望:D)
现在,使用代码隐藏:
public partial class DualButton : UserControl
{
public DualButton()
{
InitializeComponent();
}
public static readonly RoutedEvent ClickEvent =
EventManager.RegisterRoutedEvent("Click", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(DualButton));
public event RoutedEventHandler Click
{
add { AddHandler(ClickEvent, value); }
remove { RemoveHandler(ClickEvent, value); }
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
System.Windows.MessageBox.Show("Left");
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
System.Windows.MessageBox.Show("Right");
}
}
在这里,我们添加了一个RoutedEvent 以使我们的按钮真正可点击。我还添加了一些事件处理程序来检查一切是否按预期工作(确实如此!)
同样,这只是一个简单的示例,绝不是优化的。有很大的改进空间。我只是想把你推向正确的方向。
如果您还有其他问题,请随时提问。