Код XAML.<Window x:Class="Button__2_.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Button [2]" Height="455" Width="376" MinHeight="455" MinWidth="376" WindowStartupLocation="CenterScreen"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="51*" /> <RowDefinition Height="41*" /> <RowDefinition Height="44*" /> <RowDefinition Height="43*" /> <RowDefinition Height="54*" /> <RowDefinition Height="43*" /> <RowDefinition Height="47*" /> <RowDefinition Height="41*" /> <RowDefinition Height="52*" /> </Grid.RowDefinitions> <Button Content="Я обычная кнопка." Height="29" HorizontalAlignment="Stretch" Margin="15,15,15,0" Name="button1" VerticalAlignment="Top" IsEnabled="True" IsCancel="True" IsHitTestVisible="True" Click="button_Click" MouseEnter="button_MouseEnter" MouseLeave="button_MouseLeave" ClickMode="Release" Cursor="Hand" /> <Button Content="Я отключенная кнопка." Height="29" HorizontalAlignment="Stretch" IsCancel="True" IsEnabled="False" IsHitTestVisible="True" Margin="15,8,15,0" Name="button2" VerticalAlignment="Top" IsDefault="False" Grid.Row="1" Click="button_Click" MouseEnter="button_MouseEnter" MouseLeave="button_MouseLeave" Cursor="Hand" /> <Button Content="Я кнопка, повёрнутая под углом в 1 градус." HorizontalAlignment="Stretch" IsCancel="True" IsEnabled="True" IsHitTestVisible="True" Margin="15,8,12,0" Name="button3" VerticalAlignment="Top" RenderTransformOrigin="0,0" Height="29" Grid.Row="2" Click="button_Click" MouseEnter="button_MouseEnter" MouseLeave="button_MouseLeave" Cursor="Hand"> <Button.RenderTransform> <RotateTransform Angle="1" /> </Button.RenderTransform> </Button> <Button Content="Я полупрозрачная кнопка." Height="29" HorizontalAlignment="Stretch" IsCancel="True" IsEnabled="True" IsHitTestVisible="True" Margin="15,7,15,0" Name="button4" RenderTransformOrigin="0,0" VerticalAlignment="Top" Opacity="0.5" Grid.Row="3" Click="button_Click" MouseEnter="button_MouseEnter" MouseLeave="button_MouseLeave" Cursor="Hand" /> <Button Height="38" HorizontalAlignment="Stretch" IsCancel="True" IsEnabled="True" IsHitTestVisible="True" Margin="15,8,15,0" Name="button5" RenderTransformOrigin="0,0" VerticalAlignment="Top" OverridesDefaultStyle="False" Grid.Row="4" Click="button_Click" MouseEnter="button_MouseEnter" MouseLeave="button_MouseLeave"> <TextBlock Text="Я кнопка, поддерживающая перенос текста Content по словам." TextWrapping="Wrap" Cursor="Hand" /> </Button> <Button Content="Я кнопка с необычным фоном и шрифтом." Height="29" HorizontalAlignment="Stretch" IsCancel="True" IsEnabled="True" IsHitTestVisible="True" Margin="15,7,15,0" Name="button6" OverridesDefaultStyle="False" RenderTransformOrigin="0,0" VerticalAlignment="Top" FontSize="13" Grid.Row="5" Click="button_Click" MouseEnter="button_MouseEnter" MouseLeave="button_MouseLeave" Cursor="Hand"> <Button.Background> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="#FFDB0000" Offset="0" /> <GradientStop Color="#FFDE7800" Offset="0.15" /> <GradientStop Color="#FFCFE000" Offset="0.3" /> <GradientStop Color="#FF34E300" Offset="0.45" /> <GradientStop Color="#FE00E6D4" Offset="0.6" /> <GradientStop Color="#FE0000E9" Offset="0.75" /> <GradientStop Color="#FEED00ED" Offset="0.9" /> </LinearGradientBrush> </Button.Background> <Button.Foreground> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="White" Offset="0.57" /> <GradientStop Color="Black" Offset="0.494" /> </LinearGradientBrush> </Button.Foreground> </Button> <Button Content="Я кнопка с тенью." Height="29" HorizontalAlignment="Stretch" IsCancel="True" IsEnabled="True" IsHitTestVisible="True" Margin="15,8,15,0" Name="button7" VerticalAlignment="Top" Grid.Row="6" Click="button_Click" MouseEnter="button_MouseEnter" MouseLeave="button_MouseLeave" Cursor="Hand"> <Button.Effect> <DropShadowEffect /> </Button.Effect> </Button> <Button Height="29" HorizontalAlignment="Stretch" Margin="15,5,15,0" Name="button8" VerticalAlignment="Top" Grid.Row="7" Click="button_Click" MouseEnter="button_MouseEnter" MouseLeave="button_MouseLeave" Cursor="Hand"> <DockPanel> <Image Margin="0,2,0,2" Source="pack://application:,,/Photo.png" VerticalAlignment="Center"/> <TextBlock Margin="10,0,0,0" VerticalAlignment="Center">Я кнопка с картинкой.</TextBlock> </DockPanel> </Button> <Button HorizontalAlignment="Stretch" Margin="15,8,15,0" Name="button9" VerticalAlignment="Top" Grid.Row="8" Height="29" Click="button_Click" MouseEnter="button_MouseEnter" MouseLeave="button_MouseLeave" Cursor="Hand"> <DockPanel> <TextBlock VerticalAlignment="Center">Я кнопка со шрифтом с <Bold>форматированием</Bold>.</TextBlock> </DockPanel> </Button> </Grid> </Window>
Код программной части. using System; using System.Collections.Generic; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes;
namespace Button__2_ { /// <summary> /// Логика взаимодействия для MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); }
private void button_Click(object sender, RoutedEventArgs e) { Button button = (Button)sender;
if (!button.IsEnabled) { MessageBox.Show("Нажата... отключенная кнопка?! И как тебе это удалось..."); return; }
if (button == button5) { MessageBox.Show("Нажата " + (button5.Content as TextBlock).Text.Replace("Я ", "")); return; }
if (button == button8) { foreach (UIElement child in (button8.Content as DockPanel).Children) { if (child.GetType().Name == "TextBlock") { MessageBox.Show("Нажата " + (child as TextBlock).Text.Replace("Я ", "")); } } return; }
if (button == button9) { foreach (UIElement child in (button9.Content as DockPanel).Children) { MessageBox.Show("Нажата кнопка со шрифтом с форматированием."); } return; }
MessageBox.Show("Нажата " + button.Content.ToString().Replace("Я ", "")); }
private void button_MouseEnter(object sender, MouseEventArgs e) { Button button = (Button)sender;
button.Margin = new Thickness(button.Margin.Left + 2, button.Margin.Top, button.Margin.Right + 2, button.Margin.Bottom); }
private void button_MouseLeave(object sender, MouseEventArgs e) { Button button = (Button)sender;
button.Margin = new Thickness(button.Margin.Left - 2, button.Margin.Top, button.Margin.Right - 2, button.Margin.Bottom); } } }
Тэги для поисковых машин: c# example, c# sample, c# wpf example, c# wpf sample,c# wpf button sample,c# wpf button example,wpf button sample,wpf button example,c# пример,c# кнопка,c# wpf кнопка,c# button background,c# кнопка background,c# button image,c# кнопка с рисунком,c# button icon,c# rendertransform,c# rendertransform example, c# rendertransform sample,c# mouseenter example,c# mouseleave example,c# mouseenter sample,c# mouseleave sample,c# wpf sender example, c# wpf sender,c# sender,c# wpf button with formatting text,c# wpf кнопка с форматированным текстом,c# wpf button opacity, c# wpf opacity,c# button cursor, c# кнопка курсор
|