XAML Color Binding

I have a number of StackPanels each with child controls.  

I want be able to change all the controls within a given panel to the same color using bindings to the parent.  I don't actually want to change the color of the panel though.

Any ideas how I could do this ?
WatersideAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
I am not sure that I understand your question.  Do you want to store the color selection in a different property, that doesn't affect the appearance of the parent, but will change the child controls?
WatersideAuthor Commented:
I want to make a change to the parent that reflects in all it's children.

The children all have a Foreground property that I'd like to change but the parent StackPanel has no such property.
Bob LearnedCommented:
I would think that you could set the TextElement.Foreground property for the StackPanel, but there is a problem with that.  It only applies to block elements, like TextBlock.  There is a style trick that you can use to inherit the Foreground property from the parent StackPanel.

  <Window.Resources>
       <Style TargetType="{x:Type Label}">
            <Setter Property="Foreground"
            Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}, Path=(TextElement.Foreground)}"/>
        </Style>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Foreground"
            Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}, Path=(TextElement.Foreground)}"/>
        </Style>
    </Window.Resources>

    <StackPanel>
        <StackPanel TextElement.Foreground="Red">
            <Label Content="First Name" />
            <TextBlock Text="Bob" />
            <Label Content="Favorite Color" />
            <TextBox Text="Red" />

        </StackPanel>
        <StackPanel TextElement.Foreground="Purple">
            <Label Content="First Name" />
            <TextBlock Text="Jennifer" />
            <Label Content="Favorite Color" />
            <TextBox Text="Purple" />
        </StackPanel>
    </StackPanel>
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

WatersideAuthor Commented:
I don't seem to have a TextElement.Foreground property for the StackPanel.

Could I perhaps store the brush color in the StackPanel's Tag property and use a converter ?

P.S.  I know nothing about converters (yet:)
Bob LearnedCommented:
1)  What is your target .NET framework version (I am using 4.5)?

2) Where are you looking for the TextElement.Foreground property?  

3) You can try grabbing the XAML text, and pasting it into a test window.
WatersideAuthor Commented:
Microsoft Visual Studio Express 2012 for Windows Phone
Version 11.0.61030.00 Update 4
Microsoft .NET Framework
Version 4.6.00081
Bob LearnedCommented:
If this is a Windows Phone application, are you working with Windows Phone 8?  I don't have any experience with WP8 app development,

Let's find out if you can use ContentControl.

<ContentControl Foreground="Red">
    <StackPanel>
            <Label Content="First Name" />
            <TextBlock Text="Bob" />
            <Label Content="Favorite Color" />
            <TextBox Text="Red" />
    </StackPanel>
</ContentControl>

Open in new window

WatersideAuthor Commented:
Strange that having looked at a lot of WP code, I have never seen a ContentControl used !

The good news is that it does what I'm looking for without any need to specify bindings.  The bad news is that it doesn't want to work with Button.Foreground.
Bob LearnedCommented:
I am curious now what visual problem that you are trying to solve here.  What does your screen look like?  It sounds like some type of theme issue.

Buttons have a strange behavior in WPF, and I am always trying to find ways to override that behavior.

You can try to set the foreground color from the ContentControl parent:

  <Style TargetType="{x:Type Button}">
            <Setter Property="Foreground"
            Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContentControl}}, Path=(Foreground)}"/>
        </Style>
WatersideAuthor Commented:
The StackPanels show data relating to various map routes, such as the start time, the route name, the plot shape and colour.

When a user changes the display color of a route the controls on that stackpanel should change to that color.

I'm quite new to binding and thought this would be a good candidate.  Maybe I should build the panels in code behind.
Bob LearnedCommented:
Take a look at dynamic resources, as explained here:

WPF: StaticResource vs. DynamicResource
http://www.codeproject.com/Articles/393086/WPF-StaticResource-vs-DynamicResource

The difference between StaticResource and DynamicResource lies in how the resources are retrieved by the referencing elements.  StaticResource are retrieved only once by the referencing element and used for entire life of the resource.  On the other hand, DynamicResource are acquired every time the referenced object is used.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.