Binding to Metro Theme AccentColor in code behind

3 posts, 0 answers
  1. Shawn
    Shawn avatar
    10 posts
    Member since:
    Mar 2011

    Posted 30 Oct 2012 Link to this post

    Is it possible to bind to the Metro Theme AccentColor in code behind?

    I found it easy to do in XAML:
    <SolidColorBrush x:Key="AccentBrush" Color="{Binding Source={StaticResource MetroColors}, Path=Palette.AccentColor}" />

    Setting the color using radGrid.BorderBrush = new SolidColorBrush(MetroColors.PaletteInstance.AccentColor); does not update the radGrid's BorderBrush when the AccentColor is changed.
    In code behind I'm get the exception "Cannot find a Resource with the Name/Key MetroColors [Line: 10 Position: 36]" when I try to use the following binding:
                Binding accentColor = new Binding("Palette.AccentColor");
                accentColor.Source = App.Current.Resources["MetroColors"];
                accentColor.Converter = new ColorToBrushConverter();
                radGrid.SetBinding(RadGridView.BorderBrushProperty, accentColor);

  2. Yoan
    Yoan avatar
    1128 posts

    Posted 02 Nov 2012 Link to this post

    Hello Shawn,

     It's not necessary to bind to the Metro Theme in code behind. Actually, if you want to change some properties of a specific control, based on Metro Theme, you can define a style based on the theme and change the colors in it. Check the following code snippet for reference:

      <Style x:Key="s1" TargetType="telerik:RadGridView" telerik:StyleManager.BasedOn="Metro">
        <Setter Property="BorderBrush" Value="Orange"/>
      <Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource SampleDataSource}}">
        <telerik:RadGridView telerik:StyleManager.Theme="Metro" Style="{StaticResource s1}" ItemsSource="{Binding Collection}"/>

    Kind regards,
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. Shawn
    Shawn avatar
    10 posts
    Member since:
    Mar 2011

    Posted 14 Nov 2012 Link to this post

    Hi Yoan,

    Thank you for the reply. I think this answers my question, what you're saying is it's not possible to do this in the code-behind.

    Also, thank you for the XAML example.
Back to Top