[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

Button Background changes in WPF

uncaged

11/5/2008 4:11:00 PM

I want to change a button's background for mouse-over and click. My
mouse-over trigger changes the background for a moment before it changes to
the default, and my click trigger never works. I'm running on Vista with
Aero, and I suspect that ButtonChrome is getting in my way.

<Window x:Class="CashDeskClient.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presenta...
xmlns:x="http://schemas.microsoft.com/winfx/2006/...
Title="Style Test" Height="300" Width="453"
WindowStartupLocation="CenterScreen">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button Grid.Column="0">Button 1</Button>
<Button Grid.Column="1" Focusable="False">
<Button.Style>
<Style>

<!-- The following background works: -->
<Setter Property="Button.Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="White" Offset="0.0" />
<GradientStop Color="Green" Offset="0.5" />
<GradientStop Color="Green" Offset="1.0" />
</LinearGradientBrush>
</Setter.Value>
</Setter>

<Style.Triggers>

<!-- The following IsMouseOver trigger works for a moment, then the
background changes to the default for mouse-over. -->
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter Property="Button.Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="White" Offset="0.0" />
<GradientStop Color="Red" Offset="0.5" />
<GradientStop Color="Red" Offset="1.0" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>

<!-- The following IsPressed trigger never changes the background to
yellow. Instead, I see the default pressed background. -->
<Trigger Property="Button.IsPressed" Value="True">
<Setter Property="Button.Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="White" Offset="0.0" />
<GradientStop Color="Yellow" Offset="0.5" />
<GradientStop Color="Yellow" Offset="1.0" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>

</Style.Triggers>
</Style>
</Button.Style>
Button 2
</Button>
<Button Grid.Column="2" Focusable="False">Button 3</Button>
</Grid>
</Window>


Thanks.

2 Answers

v-zhye

11/7/2008 7:43:00 AM

0

Hi uncaged,

Thank you for using Microsoft Managed Newsgroup Service, my name is Zhi-Xin
Ye, I'm assigned to help you on this issue.

The actual appearance of the Button is dependent on which theme is active
on the user's system, the buttons use a default template for rendering.
However, we can create our own template to overwrite the default style and
behaviour of the buttons.

A simple sample for your information. (To make it work, you have to add a
reference to PresentationFramework.Aero.Dll ).

========= Sample Code ====================

<Window x:Class="WpfSample1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presenta...
xmlns:x="http://schemas.microsoft.com/winfx/2006/...

xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assem
bly=PresentationFramework.Aero"
Title="Window1" Height="300" Width="300"
WindowStartupLocation="CenterScreen">
<Grid>
<Grid.Resources>
<ControlTemplate x:Key="myTemplate" TargetType="{x:Type
ButtonBase}">
<Microsoft_Windows_Themes:ButtonChrome
x:Name="Chrome"
SnapsToDevicePixels="True"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}">
<ContentPresenter
HorizontalAlignment="{TemplateBinding
HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding
VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding
SnapsToDevicePixels}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
RecognizesAccessKey="True"/>
</Microsoft_Windows_Themes:ButtonChrome>
</ControlTemplate>
</Grid.Resources>
......
<Button Grid.Column="1" Focusable="False" Name="button2"
Template="{DynamicResource myTemplate}" >

<Button.Style>
......
</Button.Style>
</Button>
.....
</Grid>
</Window>

===================================

Documents for your information:

ButtonChrome Class
http://msdn.microsoft.com/en-us/library/microsoft.windows.themes.b...
e.aspx

ControlTemplate Class
http://msdn.microsoft.com/en-us/library/system.windows.controls.co...
ate.aspx

If anything is unclear, please feel free to let me know.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#not....

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa9...
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


v-zhye

11/11/2008 10:05:00 AM

0

Hi uncaged,

How about this issue now? If you still need any help or have any concern,
please feel free to feedback, thanks.

Have a nice day!

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.