C# WPF将3个对象组合成一个区域按钮
首先,我为我的英语道歉 我正在制作一个简单的C#WPF应用程序。我在MainWindow.xaml中添加了一个椭圆,label1,label2 我想把这3个对象(椭圆、label1、label2)做成一个按钮。我希望这个按钮加载我的第二个wpf页面,称为“大小”。据我所知,它应该是一个区域按钮。我曾尝试将“border”(来自WPF控件)与“MouseDown”(来自事件处理程序)结合使用,但效果不是很好。有什么建议吗?我该怎么办 我用于事件处理程序的代码是:C# WPF将3个对象组合成一个区域按钮,c#,wpf,xaml,button,C#,Wpf,Xaml,Button,首先,我为我的英语道歉 我正在制作一个简单的C#WPF应用程序。我在MainWindow.xaml中添加了一个椭圆,label1,label2 我想把这3个对象(椭圆、label1、label2)做成一个按钮。我希望这个按钮加载我的第二个wpf页面,称为“大小”。据我所知,它应该是一个区域按钮。我曾尝试将“border”(来自WPF控件)与“MouseDown”(来自事件处理程序)结合使用,但效果不是很好。有什么建议吗?我该怎么办 我用于事件处理程序的代码是: private void B
private void Border_MouseDown(object sender, MouseButtonEventArgs e)
{
Size secondPage = new Size();
secondPage.Show();
this.Close();
}
您需要重新定义按钮样式,您可以使用ControlTemplate来完成。下面是一个如何编写重新定义按钮的可重用样式的示例 我还添加了一个示例,说明如何在IsPressed事件上实现颜色更改
<Window x:Class="ColorTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<LinearGradientBrush x:Key="ButtonBackground" StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="#FFD9EDFF" Offset="0"/>
<GradientStop Color="#FFC0DEFF" Offset="0.445"/>
<GradientStop Color="#FFAFD1F8" Offset="0.53"/>
</LinearGradientBrush>
<Style x:Key="SimpleButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Border Background="{StaticResource ButtonBackground}" VerticalAlignment="Stretch" CornerRadius="2" HorizontalAlignment="Stretch"/>
<Border x:Name="BorderPressed" Opacity="0" Background="Blue" VerticalAlignment="Stretch" CornerRadius="2" HorizontalAlignment="Stretch"/>
<Label Name="label1" Content="1" HorizontalAlignment="Left"></Label>
<Label Name="label2" Content="2" HorizontalAlignment="Right"></Label>
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="MainContent" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="BorderPressed" Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.2"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="BorderPressed" Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.2"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Window.Resources>
<DockPanel>
<Button Content="Button" Height="23" Name="button1" Width="75" Style="{StaticResource SimpleButtonStyle}" Click="button1_Click"/>
</DockPanel>