Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Using a value converter for a color value
13 replies. Latest Post by coughlinj on September 12, 2008.
(0)
coughlinj
Member
334 points
114 Posts
09-10-2008 1:30 AM |
Does anybody know why I can't use a Binding combined with a ValueConverter? My code works when in the case where I want to specify a Brush. But when I change it to a Color (for instance a GradientStop or the color of a SolidColorBrush) it breaks.
Try the following code its the most basic example I could come up with...
Create a basic user control. Set the Grid.Background to
Color
of course you need to create the ColorConverter and ThemeName (just define it as a string anything will do).
Here is the most basic ColorConverter class...
public class ColorConverter: IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
return Colors.Red;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
throw new NotImplementedException();
#endregion
swildermuth
Star
8320 points
1,546 Posts
09-10-2008 5:32 AM |
How does it break (e.g. Converter never gets called; fails to build; crashes SL)?
09-10-2008 1:34 PM |
In my small snippet test app I get a XamlParseException error AG_E_PARSER_BAD_PROPERTY_VALUE. In my real app its slightly different (although its use was in a generic.xaml of a silverlight control library). In this case the converter appears to get called but its TargetType parameter is a Brush (which is wrong, but I have tried returning both a Brush and a Color object neither work). Anyways via breakpoints it seems to enter an infinite loop of calling the converter for this color the page never renders. Justin
sladapter
All-Star
17445 points
3,173 Posts
09-10-2008 2:53 PM |
Try this:
return new SolidColorBrush(Colors.Red);
09-10-2008 3:23 PM |
Thanks for the reply but... I have tried that (and did again for sanity). In my small snippet test app its not even making it into the converter. Its a very small sample app to put together if you have the time to try yourself. You've already go the converter try calling that on a SolidColorBrush or as a stop in a LinearGradientBrush... these Brush's require a "Color" object vs. directly putting them in the attributes list (eg. Rectangle Fill="COLOR") is a Brush and does function properly with a Converter returning a Brush object.
Don't think I can put XAML in these pages but I'll try...
"<UserControl.Resources> <sys:String xmlns:sys="clr-namespace:System;assembly=mscorlib" x:Name="ThemeName">Standard</sys:String> <src:ColorConverter x:Name="ColorConverter" /> </UserControl.Resources> <Grid x:Name="LayoutRoot"> <Grid.Background> <SolidColorBrush Color="{Binding Converter={StaticResource ColorConverter},Source={StaticResource ThemeName}}" /> </Grid.Background> </Grid>"
Justin
09-10-2008 4:05 PM |
This works with the converter returns a Brush object:
<Grid x:Name="LayoutRoot" Background="{Binding Converter={StaticResource BackColor}, Source={StaticResource ThemeName}}" > </Grid>
This does not work (invalid Xaml). I don't know why.
<Grid x:Name="LayoutRoot"> <Grid.Background> <SolidColorBrush Color="{Binding Converter={StaticResource ColorConverter},Source={StaticResource ThemeName}}" /> </Grid.Background>
09-10-2008 4:23 PM |
First let me thank you for taking the time to help me investigate this issue:) Its very appreciated. Its nice to see I'm not crazy as you are encountering the same problems as I. The reason I must use the second method is I need to create Gradients effects with my ColorConverter. I'm trying to keep my theme manager simple by having it only provide the base colours and shades. The XAML should then form complex brushes (such as gradients) out of the colours it provides. So I need the latter to function. Justin
Allen Ch...
13862 points
1,803 Posts
09-11-2008 10:58 PM |
Hi
Currently only those who derives from FrameworkElement can be applied binding expression. Since SolidColorBrush doesn't derive from FrameworkElement we cannot use binding for it.
Could you manually assign value instead of using data binding?
09-12-2008 1:44 PM |
Hi Allen,Thanks for the response.Manually assigning the value would mean that my colour is no longer dynamic. What I am trying to accomplish is a SaaS application where the client can choose a colour theme on the fly.I have seen other implementations where resources are added to the Application's Resource Dictionary before the Application Root is added however this is not an ideal solution either as the Application root must be reloaded each time a theme is selected meaning the current state of the application is lost.Justin
09-12-2008 1:51 PM |
coughlinj,
If dynamically changing the theme is what you want, have you taken a look at lee's blog: http://leeontech.wordpress.com/2008/07/21/dynamic-styles
I don't think it reload application when the theme is changed in this demo.
09-12-2008 2:32 PM |
Hi sladapter,
Unfortunetly he does essentially reload the application each time. You'll notice in his demo if you select an item from the list box and then change the theme the selection is lost.
Also from briefly reading the post it appears to be very similar to http://www.nikhilk.net/Silverlight-Themes.aspx. I think it does a slightly better job of explaining the process. There are some great techniques to be learned from these demo's but it does not solve my problem.
This is why I attempted the Binding method. My thinking was that the property changed notification would force the color to reload live. But I appear to have been trumped by this whole thing of Brush's not being bindable:| Sadly it looks like I'm going to need to compromise something here.
Thanks,
09-12-2008 2:39 PM |
Yes, you are right.
I went to the demo first, I changed some data in the DataGrid, then switched theme. The data kept the same as I changed. So I thought the new theme is applied without reload. Now I downloaded the code and saw the new Page is created after he set the theme.
09-12-2008 2:45 PM |
No problem. All responses are helpful.
09-12-2008 6:50 PM |
If you're interested in this thread then the following is probably of some interest as well...
http://silverlight.net/forums/p/26578/90894.aspx#90894