Skip to main content

Microsoft Silverlight

Unanswered Question Using a value converter for a color valueRSS Feed

(0)

coughlinj
coughlinj

Member

Member

334 points

114 Posts

Using a value converter for a color value

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="{Binding Converter={StaticResource ColorConverter},Source={StaticResource ThemeName}}"

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
swildermuth

Star

Star

8320 points

1,546 Posts

Re: Using a value converter for a color value

How does it break (e.g. Converter never gets called; fails to build; crashes SL)?

(If this has answered your question, "Mark as Answer")

Shawn Wildermuth
C# MVP, MCSD, Speaker and Author

Silverlight 3 Workshop
Miami, FL: Oct 12-14th
Portlant, OR: Dec 2-4th
Atlanta, GA: Dec 7-9th
http://silverlight-tour.com

coughlinj
coughlinj

Member

Member

334 points

114 Posts

Re: Using a value converter for a color value

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
sladapter

All-Star

All-Star

17441 points

3,172 Posts

Re: Using a value converter for a color value

 Try this:

public class ColorConverter: IValueConverter

{

#region IValueConverter Members

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

{

return new SolidColorBrush(Colors.Red);

}

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

coughlinj
coughlinj

Member

Member

334 points

114 Posts

Re: Using a value converter for a color value

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

sladapter
sladapter

All-Star

All-Star

17441 points

3,172 Posts

Re: Using a value converter for a color value

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>

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

coughlinj
coughlinj

Member

Member

334 points

114 Posts

Re: Using a value converter for a color value

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 Chen – MSFT
Allen Ch...

Star

Star

13862 points

1,803 Posts

Re: Using a value converter for a color value

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?

Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

coughlinj
coughlinj

Member

Member

334 points

114 Posts

Re: Using a value converter for a color value

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

sladapter
sladapter

All-Star

All-Star

17441 points

3,172 Posts

Re: Using a value converter for a color value

 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.

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

coughlinj
coughlinj

Member

Member

334 points

114 Posts

Re: Using a value converter for a color value

 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,

Justin

sladapter
sladapter

All-Star

All-Star

17441 points

3,172 Posts

Re: Using a value converter for a color value

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.

 

 

 

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

coughlinj
coughlinj

Member

Member

334 points

114 Posts

Re: Using a value converter for a color value

No problem. All responses are helpful.

coughlinj
coughlinj

Member

Member

334 points

114 Posts

Re: Using a value converter for a color value

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

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities