Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to redesign the toggle button to new one
but when i change the width and the height in XMAL it change some but the other is not change

how can i bind the width and the height and make it work fine in any width and height the user entered
this is Picture of what i am Facing Picture

by the way i am newww in WPF

thanks

What I have tried:

<Window.Resources>

    <Style x:Key="FocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle
                        Margin="2"
                        RadiusX="22"
                        RadiusY="22"
                        SnapsToDevicePixels="true"
                        Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"
                        StrokeDashArray="1 2"
                        StrokeThickness="1" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD" />
    <SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070" />
    <SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD" />
    <SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1" />
    <SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6" />
    <SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B" />
    <SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4" />
    <SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5" />
    <SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383" />

    <Style x:Key="ToggleButtonStyle1" TargetType="{x:Type ToggleButton}">
        <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}" />
        <Setter Property="Background" Value="{StaticResource Button.Static.Background}" />
        <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}" />
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
        <Setter Property="BorderThickness" Value="1" />
        <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}">
                    <Grid x:Name="ToggleButtonGrid" SnapsToDevicePixels="true" >

                        <Border
                            x:Name="border"
                            Background="Transparent"
                            CornerRadius="25" />

                        <Rectangle
                            x:Name="ToggleButtonBack"
                            Fill="#FFF4F4F5"
                            RadiusX="26"
                            RadiusY="26"
                            Stroke="DimGray"
                            StrokeThickness="0.2" />

                        <Ellipse
                            x:Name="Dot"
                            Width="40"
                            Height="40"
                            Margin="5,0,0,0"
                            HorizontalAlignment="Left"
                            Fill="White"
                            Stroke="DimGray"
                            StrokeThickness="0.2" />


                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Button.IsDefaulted" Value="true">
                            <Setter TargetName="border" Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="border" Property="Background" Value="{StaticResource Button.MouseOver.Background}" />
                            <Setter TargetName="border" Property="BorderBrush" Value="{StaticResource Button.MouseOver.Border}" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter TargetName="border" Property="Background" Value="{StaticResource Button.Pressed.Background}" />
                            <Setter TargetName="border" Property="BorderBrush" Value="{StaticResource Button.Pressed.Border}" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="border" Property="Background" Value="{StaticResource Button.Disabled.Background}" />
                            <Setter TargetName="border" Property="BorderBrush" Value="{StaticResource Button.Disabled.Border}" />
                        </Trigger>

                        <Trigger Property="IsChecked" Value="true">
                            <Setter TargetName="Dot" Property="Margin" Value="55,0,0,0" />
                            <Setter TargetName="ToggleButtonBack" Property="Fill" Value="#FF3AEC62" />
                        </Trigger>
                        <Trigger Property="IsChecked" Value="False">
                            <Setter TargetName="Dot" Property="Margin" Value="5,0,0,0" />
                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</Window.Resources>
<Grid>
    <ToggleButton
        x:Name="toggleButton"
        Width="100"
        Height="50"
        Margin="10"
        HorizontalAlignment="Left"
        VerticalAlignment="Top"
        Content="ToggleButton"
        Style="{DynamicResource ToggleButtonStyle1}" />

</Grid>
Posted
Updated 12-Jul-20 0:17am

1 solution

It seems to me that your problem is you are using fixed sizes for your slider inside the ToggleButton. So that, when the button size is increased, the slider range does not change in proportion. To solve this you can place the elements in separate cells inside a Grid and use percentages to resize the elements as the Grid size changes. But there is a more simple approach. Don't trigger the Margin size to change the sider's positon, trigger the HorizontalAlignment. Avoid setting any absolute values in the Elipse Something like this.


 <Ellipse
x:Name="Dot"
Width="{Binding ActualHeight, ElementName= ToggleButtonGrid, Mode=OneWay}"
Stretch="Uniform"
Margin="5"
HorizontalAlignment="Left"
Fill="White"
Stroke="DimGray"
StrokeThickness="0.2" / >

 <Trigger Property="IsChecked" Value="true" >
     <Setter TargetName="Dot" Property="HorizontalAlignment" Value="Right" / >
     <Setter TargetName="ToggleButtonBack" Property="Fill" Value="#FF3AEC62" / >
 </Trigger >
 <Trigger Property="IsChecked" Value="False" >
     <Setter TargetName="Dot" Property="HorizontalAlignment" Value="Left" / >
 </Trigger >
 
Share this answer
 
v2
Comments
Eng Mohamed Bassuny 16-Jul-20 2:51am    
Hi , Thanks for trying to Help me

Using "Binding ActualHeight" it give to the Ellipse the full height but it is smaller than the ActualHeight of "ToggleButtonGrid"
this maybe solve he problem of resizing but give the Ellipse not good shape with the hole button

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900