WPF Кнопка

2075 / WPF / Кнопка

 

<Button x:Name="b1" Content="" />
 
if (b1.Content.ToString() == "")
{ 
  b1.Content = "X"; 
}
 
 
Кнопка з кількома лініями тексту

<Button Width="100">
  <TextBlock TextWrapping="Wrap">This is a long button label</TextBlock>
</Button>
 
 
Визвати подію кнопки

B1.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
Button_Click(sender, e);

 
Зміна позиції іншого об'єкту (код)

label.Margin = new Thickness(label.Margin.Left+10, label.Margin.Top, 0, 0);
 
 
Зміна кольору при наведенні

<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="50" Height="50" HorizontalContentAlignment="Left" BorderBrush="{x:Null}" Foreground="{x:Null}" Margin="50,0,0,0">
 <Button.Style>
  <Style TargetType="{x:Type Button}">
   <Setter Property="Background" Value="Green"/>
   <Setter Property="Template">
    <Setter.Value>
     <ControlTemplate TargetType="{x:Type Button}">
      <Border Background="{TemplateBinding Background}">
        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
      </Border>
     </ControlTemplate>
    </Setter.Value>
   </Setter>
   <Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
     <Setter Property="Background" Value="Blue"/>
    </Trigger>
   </Style.Triggers>
  </Style>
 </Button.Style>
</Button>
 
 
Зміна кольору після наведення

<Button Content="Content" Background="Red">
 <Button.Triggers>
  <EventTrigger RoutedEvent="MouseEnter">
   <BeginStoryboard>
    <Storyboard>
     <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="CadetBlue"/>
    </Storyboard>
   </BeginStoryboard>
  </EventTrigger>
 </Button.Triggers>
</Button>
 
 
Зміна кольору після натискання

<Button Content="Content" Background="Red">
 <Button.Style>
  <Style TargetType="Button">
   <Style.Triggers>
    <Trigger Property="IsPressed" Value="True">
     <Trigger.EnterActions>
      <BeginStoryboard>
       <Storyboard>
        <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="CadetBlue"/>
       </Storyboard>
      </BeginStoryboard>
     </Trigger.EnterActions>
    </Trigger>
   </Style.Triggers>
  </Style>
 </Button.Style>
</Button>
 
 
Кругла кнопка зі зміною фону при наведенні
Відео