none
Binding Dependency Property inside Style in a UserControl

    Question

  • Hello Everybody,

    I have a doubt regarding Dependency properties and Styles. The best way to do the question is with a example:

    I have the following dependency property that belongs to UserControl ...

            public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("AdvCornerRadius", 
                typeof(double), 
                typeof(AdvButton), 
                new PropertyMetadata((double)3));
          
            public string _CornerRadius
            {
                get { return (string)this.GetValue(CornerRadiusProperty); }
                set { this.SetValue(CornerRadiusProperty, value); }
            }

    And the style in the UserControl is defined in the its resourcers.

    <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="BaseProgram.AdvButton" x:Name="UserControl" d:DesignWidth="100" d:DesignHeight="50"> <!-- Resources ******************************************************************************* ****************************************************************************************** --> <UserControl.Resources> <Style x:Key="AdvButtonStyle" TargetType="{x:Type Button}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Grid> <Border x:Name="ButtonBorder" CornerRadius="{...Binding AdvCornerRadius...}"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>

    <!-- Controls *************************************************************************
    *********************************************************************************** -->

    <Grid>

    <Button Style="{StaticResourcer AdvButtonStyle}/>

    </Grid>

    How to bind the dependency property to CornerRadius property of the Border in the style?

    Thank a lot

    Regards.

    Boltian

    Sunday, June 24, 2012 8:10 AM

Answers

  • you could try

    <Border x:Name="ButtonBorder" CornerRadius="{Binding AdvCornerRadius,ElementName=UserControl}"/>

    • Marked as answer by Boltian Monday, June 25, 2012 2:24 PM
    Sunday, June 24, 2012 8:50 AM

All replies

  • you could try

    <Border x:Name="ButtonBorder" CornerRadius="{Binding AdvCornerRadius,ElementName=UserControl}"/>

    • Marked as answer by Boltian Monday, June 25, 2012 2:24 PM
    Sunday, June 24, 2012 8:50 AM
  • Thank you for your answer David.Handke,

    But unfurtunately, I have the following issues:

    1- Whe I generate the solution, the instiantated UserControl updates the radius and all is OK.

    But if i try to change the dependency property '_CornerRadius', the IDE give me an exeption 'The value of property is not valid' and in the graphic representation of the UserControl appears a white square with a red cross.

    2- The other issue is that I would like to the changes in the UserControl in the design phase. Is There any solution for this topic?

    Thanks

    Boltian

    Sunday, June 24, 2012 4:46 PM
  • Hi,

    the setter of your prop _CornerRadius looks a bit odd. As this property is of type string, the "value" can't be passed as parameter in SetValue of a DependencyProperty of type double. Additionally there are assumptions about the naming of DependencyProperties (and their registration). The property is expected to have the registered name plus "Property" added, but I'm not quite sure, how much this is a "MUST" or just convention. So you might try this

    public static readonly DependencyProperty CornerRadiusProperty =
        DependencyProperty.Register("CornerRadius", typeof(double), typeof(AdvButton), new UIPropertyMetadata(3d));
    
    public double CornerRadius
    {
       get { return (double)this.GetValue(CornerRadiusProperty); }
       set { this.SetValue(CornerRadiusProperty, value); }
    }
    
    public string _CornerRadius
    {
       get { return this.CornerRadius.ToString(); }
       set
       {
          double cornerRadius;
          if (double.TryParse(value, out cornerRadius))
          {
             this.CornerRadius = cornerRadius;
          }
          else
          {
             this.CornerRadius = double.NaN;
          }
       }
    }

    Or you rename every "CornerRadius" to "AdvCornerRadius".

    Cheers
    Jürgen

    • Proposed as answer by LeeCampbell Monday, June 25, 2012 2:27 PM
    Sunday, June 24, 2012 5:55 PM
  • Sorry, thanks to Jürgen Röhr, I have seen that I have made a sistake in code. If I modify it with...:

           public double _CornerRadius
            {
                get { return (double)this.GetValue(CornerRadiusProperty); }
                set { this.SetValue(CornerRadiusProperty, value); }
            }

    The David.Handke's solution works fine.

    Thank you both for your support.

    Boltian

    PD:  Know anybody how to automate the update graphic representation in design mode of the UserControl (no when this one is instiantated)?

    Monday, June 25, 2012 2:22 PM