SV: [DOTNET-WINFORMS] Styling multi content buttons in WPF

Benny Skjold Tordrup <[email protected]> Sat, 28 Jun 2008 21:07:55 +0200
Newsgroups gmane.comp.windows.devel.dotnet.winforms
Message-ID <014301c8d952$455d7ec0$d0187c40$@net>
Thank you James - at first look, it appears to do what I want.

I'll dive a bit more into this method and try to apply it where I'm
experimenting with it now.

Med venlig hilsen / Best regards

Benny



-----Oprindelig meddelelse-----
Fra: Discussion forum for developers using Windows Forms to build apps =
and
controls [mailto:[email protected]] P=E5 vegne af =
James
Geall
Sendt: 28. juni 2008 16:55
Til: [email protected]
Emne: Re: [DOTNET-WINFORMS] Styling multi content buttons in WPF

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=3D"http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys=3D"clr-namespace:System;assembly=3Dmscorlib"
xmlns:x=3D"http://schemas.microsoft.com/winfx/2006/xaml" >
        <StackPanel>
        <StackPanel.Resources>
            <Style x:Key=3D"InheritFromButton" TargetType=3D"{x:Type
ContentControl}"> <!--Does not work for disabled-->
                <Setter Property=3D"Foreground" Value=3D"{Binding
RelativeSource=3D{RelativeSource Mode=3DFindAncestor, =
AncestorType=3D{x:Type
ContentControl}}, Path=3DForeground}"/>
            </Style>
            <Style TargetType=3D"{x:Type Button}" >
                <Style.Triggers>
                    <Trigger Property=3D"IsEnabled" Value=3D"false">
                        <Setter Property=3D"Foreground" Value=3D"Blue" =
/>
                    </Trigger>
                    <Trigger Property=3D"IsEnabled" Value=3D"true">
                        <Setter Property=3D"Foreground" Value=3D"Yellow" =
/>
                    </Trigger>
                    <Trigger Property=3D"IsMouseOver" Value=3D"true" >
                        <Setter Property=3D"Foreground" Value=3D"Green" =
/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </StackPanel.Resources>
        <CheckBox x:Name=3D"ToggleEnabled">Button Enabled</CheckBox>
        <Button Grid.Row=3D"1" Grid.Column=3D"1" IsEnabled=3D"{Binding
ElementName=3DToggleEnabled, Path=3DIsChecked}">
            <Button.Content>
                <Grid>
                    <StackPanel Orientation=3D"Horizontal">
                        <Label Foreground=3D"{Binding
RelativeSource=3D{RelativeSource Mode=3DFindAncestor, =
AncestorType=3D{x:Type
ContentControl}}, Path=3DForeground}">Method 1</Label>
                        <Label Content=3D"Method 2"
Style=3D"{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