Re: Styling multi content buttons in WPF
James Geall <[email protected]> Sat, 28 Jun 2008 15:54:53 +0100
| Newsgroups | gmane.comp.windows.devel.dotnet.winforms |
|---|---|
| Message-ID | <[email protected]> |
you could bind the controls that you want to change to the buttons
foreground property using a relative source
I have included an example below, watch out for wrapping.
There are 2 methods in the example. Setting the property binding
using the style does not work for the disabled foreground color, but
setting the binding on the control itself does,
HTH
James
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<StackPanel>
<StackPanel.Resources>
<Style x:Key="InheritFromButton" TargetType="{x:Type
ContentControl}"> <!--Does not work for disabled-->
<Setter Property="Foreground" Value="{Binding
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type
ContentControl}}, Path=Foreground}"/>
</Style>
<Style TargetType="{x:Type Button}" >
<Style.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="Blue" />
</Trigger>
<Trigger Property="IsEnabled" Value="true">
<Setter Property="Foreground" Value="Yellow" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true" >
<Setter Property="Foreground" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<CheckBox x:Name="ToggleEnabled">Button Enabled</CheckBox>
<Button Grid.Row="1" Grid.Column="1" IsEnabled="{Binding
ElementName=ToggleEnabled, Path=IsChecked}">
<Button.Content>
<Grid>
<StackPanel Orientation="Horizontal">
<Label Foreground="{Binding
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type
ContentControl}}, Path=Foreground}">Method 1</Label>
<Label Content="Method 2"
Style="{StaticResource InheritFromButton}" />
</StackPanel>
</Grid>
</Button.Content>
</Button>
</StackPanel>
</Page>
On Sat, Jun 28, 2008 at 11:41 AM, Benny Skjold Tordrup
<[email protected]> wrote:
>
> Hi all
>
> I have a style for at WPF button that changes the foreground color of the
> button content when the button is disabled or pressed. This works fine if I
> only add a text to the contents or use a single control as content.
>
> If I then use a Grid as content in the button in order to make a more
> sophisticated content, the above tricks do not work. I assume that it is
> because WPF tries to set the foreground property directly on a single
> control content - which I do not have in this case.
>
> Any ideas?
>
> Med venlig hilsen / Best regards
>
> Benny Tordrup