How to set Togglebutton''s background where it has checked
hi.all
How to set the Togglebutton's background where it has checked?
thanks for any advise
hi.all
How to set the Togglebutton's background where it has checked?
thanks for any advise
<Window.Resources>
<Style TargetType="ToggleButton" x:Key="checkedToggle">
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<ToggleButton Style="{StaticResource checkedToggle}" />
regards
HI,Marlon Grech
Thank for you sample xaml, but it seems not work
or I miss something?
How to set background with code where it checked?
Private Sub ToggleButton1_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles ToggleButton1.Checked, ToggleButton2.Checked
Dim tb As ToggleButton = sender
tb.Background = Brushes.LightBlue ' the line not work too
End Sub
so the style that I gave you did not work?
can you please post the code that you are using in XAML
check to see if the conversion "Dim tb As ToggleButton = sender" succedes? if not the next line will fail because tb is nothing in this case
{
InitializeComponent();
ToggleButton t = new ToggleButton();t.Content =
"Toogle button";t.Checked +=
new RoutedEventHandler(t_Checked);this.Content = t;}
void t_Checked(object sender, RoutedEventArgs e){
ToggleButton t = sender as ToggleButton;t.Background =
Brushes.LightBlue;}
this is an example I tested.If this is not working provide the xaml code where you declared the toggle button.
I create a new project to test this ,with Orcas beta2,Windows2003 +sp2
this is my code
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<ToggleButton Content="togbut1" Margin="114,116,50,116" Name="ToggleButton1" />
<ToggleButton Content="togbut2" Margin="114,0,50,64" Name="ToggleButton2" Height="30" VerticalAlignment="Bottom" />
</Grid>
</Window>
Class Window1
Private Sub ToggleButton1_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles ToggleButton1.Checked
sender.background = Brushes.Red 'not work
End Sub
Private Sub ToggleButton1_Unchecked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ToggleButton1.Unchecked
sender.background = Brushes.Green 'works
End Sub
Private Sub ToggleButton2_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles ToggleButton2.Checked
sender.background = Brushes.Red 'not work ,but where ToggleButton2 has unChecked,then we can see it was changed to Red
End Sub
End Class
It perplexed me.
dowload this code <Window x:Class="Window1" <Style TargetType="ToggleButton" > </ToggleButton.Style> </ToggleButton> </Grid>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<ToggleButton Content="togbut1" Margin="114,116,50,116" Name="ToggleButton1" >
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Window>
Hi,Marlon Grech ,
Your xaml is not work in my computer, here is the screen shot
I give up, thank you .
<Grid>
<Grid.Resources>
<Style x:Key="ToggleButtonStyle1" TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="border" Padding="5,5,5,5" CornerRadius="5,5,5,5" Background="#FFBFACAC" BorderBrush="#FF000000" BorderThickness="1,1,1,1" SnapsToDevicePixels="True">
<ContentPresenter x:Name="contentPresenter"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter Property="Background" TargetName="border" Value="#FFC31010"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<ToggleButton Content="ToggleButton" Margin="25" Width="90" Height="45" Style="{StaticResource ToggleButtonStyle1}"/>
</Grid>
"Aquaseal" approch will not follow the user's Windows theme (classic, Luna, Aero)..
Here is another approch. Wich use the ContentTemplate to display the red background:
<Window.Resources>
<DataTemplate x:Key="RedBackground">
<Style TargetType="ToggleButton" x:Key="checkedToggle">
<ToggleButton Content="Test" Style="{StaticResource checkedToggle}"/>
Try this...
Please mark me as answerer if this post helped you.