Waterside
asked on
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 ?
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 ?
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?
ASKER
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.
The children all have a Foreground property that I'd like to change but the parent StackPanel has no such property.
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={RelativeSo urce AncestorType={x:Type StackPanel}}, Path=(TextElement.Foregrou nd)}"/>
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Foreground"
Value="{Binding RelativeSource={RelativeSo urce AncestorType={x:Type StackPanel}}, Path=(TextElement.Foregrou nd)}"/>
</Style>
</Window.Resources>
<StackPanel>
<StackPanel TextElement.Foreground="Re d">
<Label Content="First Name" />
<TextBlock Text="Bob" />
<Label Content="Favorite Color" />
<TextBox Text="Red" />
</StackPanel>
<StackPanel TextElement.Foreground="Pu rple">
<Label Content="First Name" />
<TextBlock Text="Jennifer" />
<Label Content="Favorite Color" />
<TextBox Text="Purple" />
</StackPanel>
</StackPanel>
<Window.Resources>
<Style TargetType="{x:Type Label}">
<Setter Property="Foreground"
Value="{Binding RelativeSource={RelativeSo
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Foreground"
Value="{Binding RelativeSource={RelativeSo
</Style>
</Window.Resources>
<StackPanel>
<StackPanel TextElement.Foreground="Re
<Label Content="First Name" />
<TextBlock Text="Bob" />
<Label Content="Favorite Color" />
<TextBox Text="Red" />
</StackPanel>
<StackPanel TextElement.Foreground="Pu
<Label Content="First Name" />
<TextBlock Text="Jennifer" />
<Label Content="Favorite Color" />
<TextBox Text="Purple" />
</StackPanel>
</StackPanel>
ASKER
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:)
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:)
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.
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.
ASKER
Microsoft Visual Studio Express 2012 for Windows Phone
Version 11.0.61030.00 Update 4
Microsoft .NET Framework
Version 4.6.00081
Version 11.0.61030.00 Update 4
Microsoft .NET Framework
Version 4.6.00081
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.
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>
ASKER
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.
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.
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={RelativeSo urce AncestorType={x:Type ContentControl}}, Path=(Foreground)}"/>
</Style>
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={RelativeSo
</Style>
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.