locked
Bind a resource value to a variable RRS feed

  • Question

  • I've been looking around and I've tried to adapt others questions to my situation with no luck thus far, so I decided its best to just ask.  Here's my situation.  I have the following to build my grip

    <Window.Resources>
            <SolidColorBrush x:Key="ResizeGripperForeground" Color="{Binding Path=themeColor}"/>
            <Style TargetType="{x:Type ResizeGrip}">
                <Setter Property="MinWidth" Value="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"/>
                <Setter Property="MinHeight" Value="{DynamicResource {x:Static SystemParameters.HorizontalScrollBarHeightKey}}"/>
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="Foreground" Value="Red"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ResizeGrip}">
                            <Grid SnapsToDevicePixels="True" Background="{TemplateBinding Background}">
                                <Path Fill="Gray" HorizontalAlignment="Right" Margin="0,0,4,4" VerticalAlignment="Bottom" Data="M 8,0 L 10,0 L 10,2 L 8,2 Z M 4,4 L 6,4 L 6,6 L 4,6 Z M 8,4 L 10,4 L 10,6 L 8,6 Z M 0,8 L 2,8 L 2,10 L 0,10 Z M 4,8 L 6,8 L 6,10 L 4,10 Z M 8,8 L 10,8 L 10,10 L 8,10 Z"/>
                                <Path Fill="{StaticResource ResizeGripperForeground}" HorizontalAlignment="Right" Margin="0,0,5,5" VerticalAlignment="Bottom" Data="M 8,0 L 10,0 L 10,2 L 8,2 Z M 4,4 L 6,4 L 6,6 L 4,6 Z M 8,4 L 10,4 L 10,6 L 8,6 Z M 0,8 L 2,8 L 2,10 L 0,10 Z M 4,8 L 6,8 L 6,10 L 4,10 Z M 8,8 L 10,8 L 10,10 L 8,10 Z"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="Visibility" Value="Hidden"/>
            </Style>
        </Window.Resources>


    Then for the C# I have 

    public Color themeColor;

    Now I plan to change the color depending on user options but can't seem to figure out how to get the two to talk to each other.  Any help would be GREATLY appreciated.  Thank you in advance!


    Trust, but verify.

    Wednesday, May 14, 2014 8:40 AM

Answers

  • You could change the value of the Color property of the ResizeGripperForeground resource directly instead of binding to another property:

    <SolidColorBrush x:Key="ResizeGripperForeground"/>

    SolidColorBrush brush = this.Resources["ResizeGripperForeground"] as SolidColorBrush;
    brush.Color = Colors.Red; // = themeColor

    Edit: If you necessarily want to use binding, you must bind to a property and not a field. You could make the themeColor a dependency property or a CLR property. If you make it a CLR property, the window needs to implement the INotifyPropertyChanged interface and raise its PropertyChanged event whenever you change the value of the property for the view to automatically reflect the changes:

    <SolidColorBrush x:Key="ResizeGripperForeground" Color="{Binding Path=themeColor, RelativeSource={RelativeSource AncestorType=Window}}"/>

    public partial class MainWindow : Window, INotifyPropertyChanged
     {
    
      private Color _color;
      public Color themeColor
      {
       get { return _color; }
       set
       { 
        _color = value;
        if (PropertyChanged != null)
         PropertyChanged(this, new PropertyChangedEventArgs("themeColor"));
       }
      }
    
      public event PropertyChangedEventHandler PropertyChanged;
    
      
     }
    



    • Edited by Magnus (MM8)MVP Wednesday, May 14, 2014 9:30 AM
    • Proposed as answer by OlofPetterson Wednesday, May 14, 2014 10:18 AM
    • Marked as answer by Lightor Saturday, May 17, 2014 3:19 AM
    Wednesday, May 14, 2014 9:02 AM

All replies

  • You could change the value of the Color property of the ResizeGripperForeground resource directly instead of binding to another property:

    <SolidColorBrush x:Key="ResizeGripperForeground"/>

    SolidColorBrush brush = this.Resources["ResizeGripperForeground"] as SolidColorBrush;
    brush.Color = Colors.Red; // = themeColor

    Edit: If you necessarily want to use binding, you must bind to a property and not a field. You could make the themeColor a dependency property or a CLR property. If you make it a CLR property, the window needs to implement the INotifyPropertyChanged interface and raise its PropertyChanged event whenever you change the value of the property for the view to automatically reflect the changes:

    <SolidColorBrush x:Key="ResizeGripperForeground" Color="{Binding Path=themeColor, RelativeSource={RelativeSource AncestorType=Window}}"/>

    public partial class MainWindow : Window, INotifyPropertyChanged
     {
    
      private Color _color;
      public Color themeColor
      {
       get { return _color; }
       set
       { 
        _color = value;
        if (PropertyChanged != null)
         PropertyChanged(this, new PropertyChangedEventArgs("themeColor"));
       }
      }
    
      public event PropertyChangedEventHandler PropertyChanged;
    
      
     }
    



    • Edited by Magnus (MM8)MVP Wednesday, May 14, 2014 9:30 AM
    • Proposed as answer by OlofPetterson Wednesday, May 14, 2014 10:18 AM
    • Marked as answer by Lightor Saturday, May 17, 2014 3:19 AM
    Wednesday, May 14, 2014 9:02 AM
  • Great information and examples, thanks a ton!

    Trust, but verify.

    Saturday, May 17, 2014 3:19 AM