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

[112 byte] By [Paintshow] at [2008-1-9]
# 1

you can do the following

<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

MarlonGrech at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 2

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

Paintshow at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 3

I am not seeing any thing wrong....

so the style that I gave you did not work?

can you please post the code that you are using in XAML

MarlonGrech at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 4

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

Code Snippet

public Window1()

{

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.

cono_mm at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 5

I create a new project to test this ,with Orcas beta2,Windows2003 +sp2

this is my code

Code Snippet

<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>

Code Snippet

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
Paintshow at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 6
Code Snippet



<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.Style>

<Style TargetType="ToggleButton" >
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>

</ToggleButton.Style>

</ToggleButton>

</Grid>
</Window>


try this....
MarlonGrech at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 7

Hi,Marlon Grech ,

Your xaml is not work in my computer, here is the screen shot

I give up, thank you .

Paintshow at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 8
I couldn't get Marlon's example to work either, and when I tried doing the trigger in blend it was not working. You change the template of the ToggleButton and then change the background of that new element based on the IsChecked property as shown below:

Code Snippet

<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>



Hope that helps some.
aquaseal at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 9
"Marlon Grech" don't work because in the "Luna" style, the default ControlTemplate don't use the background property. But it should work in "classic" style...

"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">

<Grid Background="Red">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center" Text="{Binding}"/>
</Grid>
</DataTemplate>

<Style TargetType="ToggleButton" x:Key="checkedToggle">

<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="ContentTemplate" Value="{StaticResource RedBackground}"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>

<ToggleButton Content="Test" Style="{StaticResource checkedToggle}"/>

Try this...
Please mark me as answerer if this post helped you.

etiennemartin at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 10

Great,

etiennemartin and aquaseal 's sample works

Thank you all .

Best regards

Paintshow at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...

Visual Studio Orcas

Site Classified