none
Binding UserControl Property to a resource property RRS feed

  • Question

  • I have a UserControl with brush resources

    <UserControl.Resources>
      <SolidColorBrush Color="{Binding CircleColor, RelativeSource={RelativeSource Self}}" x:Key="circleBrush" />
    ...
    </UserControl.Resources>

    circleBrush is used in the UserControl to assign a brush to components

    I want CircleColor to be a Property of the UserControl (Normal or DP) defined in the code behind.

    The UserControl's DataContext is the ViewModel which is used for other bindings.

    No matter what I try it is looking to the ViewModel for the CircleColor property to bind.

    I must be missing something?

    Friday, June 8, 2012 8:23 AM

Answers

  • Hi PrismPoint,

    You can try set the SolidColorBrush as:

     <UserControl.Resources>
            <SolidColorBrush  Color="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=CircleColor}" x:Key="circleBrush" />
        </UserControl.Resources>
        <Grid>
            <StackPanel>
                <Path Name="myPath" Stroke="Red" Fill="{StaticResource circleBrush}">
                    <Path.Data>
                        <EllipseGeometry Center="50,50" RadiusX="25" RadiusY="25"/>
                    </Path.Data>
    </Path>
            </StackPanel>
        </Grid>

    And in UserControl code behind:

     public UserControl1()
            {
                InitializeComponent();
                this.CircleColor = Colors.Black;
            }
            public Color CircleColor
            {
                get { return (Color)GetValue(CircleColorProperty); }
                set { SetValue(CircleColorProperty, value); }
            }
            public static readonly DependencyProperty CircleColorProperty =
                  DependencyProperty.Register("CircleColor", typeof(Color), typeof(UserControl1));

    Hope it helps.

    Have a nice day.

    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by PrismPoint Wednesday, June 13, 2012 12:29 AM
    Monday, June 11, 2012 6:09 AM
    Moderator

All replies

  • Hi PrismPoint,

    You can try set the SolidColorBrush as:

     <UserControl.Resources>
            <SolidColorBrush  Color="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=CircleColor}" x:Key="circleBrush" />
        </UserControl.Resources>
        <Grid>
            <StackPanel>
                <Path Name="myPath" Stroke="Red" Fill="{StaticResource circleBrush}">
                    <Path.Data>
                        <EllipseGeometry Center="50,50" RadiusX="25" RadiusY="25"/>
                    </Path.Data>
    </Path>
            </StackPanel>
        </Grid>

    And in UserControl code behind:

     public UserControl1()
            {
                InitializeComponent();
                this.CircleColor = Colors.Black;
            }
            public Color CircleColor
            {
                get { return (Color)GetValue(CircleColorProperty); }
                set { SetValue(CircleColorProperty, value); }
            }
            public static readonly DependencyProperty CircleColorProperty =
                  DependencyProperty.Register("CircleColor", typeof(Color), typeof(UserControl1));

    Hope it helps.

    Have a nice day.

    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by PrismPoint Wednesday, June 13, 2012 12:29 AM
    Monday, June 11, 2012 6:09 AM
    Moderator
  • Of course you are correct. I did have that solution at one point but must have had something else going on (probably built dll but didn't rebuild main ;_) - sorry to waste time
    Wednesday, June 13, 2012 12:30 AM
  • However now I have another issue -

    When the DP is updated (CircleColor = something) the element using the resource is not updated?

    John

    Wednesday, June 13, 2012 1:43 AM