0

I am creating UWP application.I have few LinearGradientBrushes, where the color is set directly in the LinearGradientBrush reference as GradientStops. However, I want to have a predefined set of colors defined in the resource distionary that I can use a a reference for each GradientStop, so that changing the color scheme for the application is a matter of changing the values of the SolidColorBrushes:

   <!--Resource Dictionary  -->
<ResourceDictionary.ThemeDictionaries>
        <ResourceDictionary x:Key="Default">
             <SolidColorBrush x:Key="stop1" Color="#FF5A5A5A"/>
<SolidColorBrush x:Key="stop2" Color="#FF222222"/>
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
             <SolidColorBrush x:Key="stop1" Color="Black"/>
<SolidColorBrush x:Key="stop2" Color="White"/>
</ResourceDictionary>
<ResourceDictionary x:Key="HighContrast">
             <SolidColorBrush x:Key="stop1" Color="Black"/>
<SolidColorBrush x:Key="stop2" Color="White"/>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
    <!-- control Template-->
    <LinearGradientBrush x:Key="gradient">
      <GradientStop Color="{Binding Source={Themeresource stop1},Path=Color}" Offset="0"/>
      <GradientStop Color="{Binding Source={Themeresource stop2},Path=Color}" Offset="1"/>
    </LinearGradientBrush>

Its Giving error that nam/key stop1 is not Found

2 Answers2

0

The problem is stop1 is static resource but not Themeresource . So, we need edit binding source as StaticResource.

<LinearGradientBrush x:Key="gradient">
    <GradientStop Color="{Binding Source={StaticResource stop1},Path=Color}" Offset="0"/>
    <GradientStop Color="{Binding Source={StaticResource stop2},Path=Color}" Offset="1"/>
</LinearGradientBrush>

Update

For the testing, if we place above in ResourceDictionary, it will work.

<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.ThemeDictionaries>
            <ResourceDictionary x:Key="Default">
                <SolidColorBrush x:Key="stop1" Color="#FF5A5A5A"/>
                <SolidColorBrush x:Key="stop2" Color="#FF222222"/>
            </ResourceDictionary>
            <ResourceDictionary x:Key="Dark">
                <SolidColorBrush x:Key="stop1" Color="Black"/>
                <SolidColorBrush x:Key="stop2" Color="White"/>
            </ResourceDictionary>
            <ResourceDictionary x:Key="HighContrast">
                <SolidColorBrush x:Key="stop1" Color="Black"/>
                <SolidColorBrush x:Key="stop2" Color="White"/>
            </ResourceDictionary>
        </ResourceDictionary.ThemeDictionaries>
   

 
    <!-- control Template-->
    <LinearGradientBrush x:Key="gradient">
        <GradientStop Color="{Binding Source={ThemeResource stop1},Path=Color}" Offset="0"/>
        <GradientStop Color="{Binding Source={ThemeResource stop2},Path=Color}" Offset="1"/>
    </LinearGradientBrush>
    </ResourceDictionary>
</Page.Resources>
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
0

The above problem can be solved by using binding

public LinearGradientBrush GradientBrush
    {
        get { return _GradientBrush; }
        set
        {
            _GradientBrush = value;
            RaisePropertyChanged("GradientBrush");
        }
    }
 GradientBrush = GetGradientBrush();

 public static LinearGradientBrush GetGradientBrush()
    {
        var grColor1 = ((SolidColorBrush)Application.Current.Resources["stop1"]).Color;
        var grColor2 = ((SolidColorBrush)Application.Current.Resources["stop2"]).Color;
        LinearGradientBrush lgBrush = new LinearGradientBrush();
        lgBrush.GradientStops.Add(new GradientStop() { Color = grColor1, Offset = 0.1 });
        lgBrush.GradientStops.Add(new GradientStop() { Color = grColor2, Offset = 0.9 });

        lgBrush.StartPoint = new Point(0, 1);
        lgBrush.EndPoint = new Point(1, 0);

       return lgBrush;
    }

<Grid Background="{Binding GradientBrush}" >