Change togglebutton user behavior in WPF

I am new to WPF.

How can I change the behavior of togglebutton. enter image description here before enter image description here with a black bag and without borders. Need to use a control pattern?

+6
source share
2 answers

You need to change the control template or style to change the look of an existing control. Look at this sample, which is something similar to your requirements. what i did was i changed chrome (default style for windows) and created my own style using Border and Content Presenter. Then I created triggers for the style. For visualization in the mouseover and ischecked event, I change the background color at the border.

<Window.Resources> <Style x:Key="ToggleButtonStyle1" TargetType="{x:Type ToggleButton}"> <Setter Property="BorderThickness" Value="1"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> <Setter Property="HorizontalContentAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="Padding" Value="1"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ToggleButton}"> <Border x:Name="border"> <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True" TextElement.Foreground="White" HorizontalAlignment="Center"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsKeyboardFocused" Value="true"/> <Trigger Property="IsChecked" Value="true"> <Setter Property="Background" TargetName="border" Value="#FF6C6C6C"/> <Setter Property="CornerRadius" TargetName="border" Value="5"/> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" TargetName="border" Value="#FF282828"/> </Trigger> <Trigger Property="IsEnabled" Value="false"> <Setter Property="Foreground" Value="#ADADAD"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid x:Name="LayoutRoot"> <ToggleButton HorizontalAlignment="Left" Margin="136,59,0,0" Style="{DynamicResource ToggleButtonStyle1}" VerticalAlignment="Top" Width="27" Height="24" Content="-" FontSize="21.333" FontWeight="Bold" HorizontalContentAlignment="Center" Padding="0" VerticalContentAlignment="Center" IsThreeState="True"/> </Grid> 

+4
source

Yes, you want to use the ControlTemplate to change the way ToggleButton displayed. Take a look at the ToggleButton page as well as this article:

Customize the appearance of an existing control by creating a checklist

to get you started.

+4
source

Source: https://habr.com/ru/post/888328/


All Articles