Martyn Ball
Martyn Ball

Reputation: 4895

WPF Custom Property

I have added a new property to the ProgresBar control, how ever I don't think i'm doing it correctly. Below is the progressbar in the MainWindow.xaml, I need to have 2 values with a gap between them.

<ProgressBar Style="{StaticResource CircularProgress}" 
                Value="50"
                Extensions:CustomExtensions.Radius="140 0" />

Now here is my Custom Extension, I made it a string as there is a gap between the two numbers.

public static readonly DependencyProperty RadiusProperty =
    DependencyProperty.RegisterAttached("Radius", typeof(string), typeof(CustomExtensions), new PropertyMetadata(default(string)));

public static void SetRadius(UIElement element, string value)
{
    element.SetValue(RadiusProperty, value);
}

public static string GetRadius(UIElement element)
{
    return (string)element.GetValue(RadiusProperty);
}

Now here is where I use this custom property, which isn't working.

<PathFigure x:Name="pathFigure" StartPoint="{Binding Path=Radius, RelativeSource={RelativeSource TemplatedParent}}">

Really I have got 2 questions: 1. The values don't seem to be applying to my ControlTemplate, as if I remove the binding an enter 140 0 myself it shows the ArcSegment, however with the binding it doesn't.

  1. Is it possible to only type Radius for the custom property without Extensions:CustomExtensions?

Edit: When attempting to bind a textbox to this value I get this error:

Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll

Additional information: 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '36' and line position '20'.

code:

<ProgressBar Style="{StaticResource CircularProgress}" 
                     Value="{Binding Source={StaticResource runtimeVariables},Path=uploadProgress}" 
                     Extensions:CustomExtensions.Radius="80" 
                     Name="test"/>
        <TextBlock Text="{Binding ElementName=test, Path=(Extensions:CustomExtensions.Radius)}"/>

Upvotes: 0

Views: 193

Answers (2)

kirotab
kirotab

Reputation: 1306

I was wrong in my original answer, the type of the attached property was correctly defined and it should be CustomExtensions not ProgressBar

ownerType - The owner type that is registering the dependency property. MSDN Reference

You have to set to ProgressBar your attached property typeof(ProgressBar)

public static readonly DependencyProperty RadiusProperty =
    DependencyProperty.RegisterAttached(
        "Radius", typeof(string),
        typeof(CustomExtensions),
        new PropertyMetadata(default(string))
    );

Here's how you could easily test

<ProgressBar x:Name="testProgressBar"
             Value="50"
             local:CustomExtensions.Radius="140 0" />
<TextBlock Text="{Binding ElementName=testProgressBar, Path=Radius}"/>

Upvotes: 1

Stipo
Stipo

Reputation: 4606

You must use special syntax if binding to attached property (with parentheses around the attached property). Also, you should specify a converter to convert string to a Point.

Here is an example:

<PathFigure x:Name="pathFigure" StartPoint="{Binding Path=(Extensions:CustomExtensions.Radius), RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource pointConverter}">

Edit: I was unable to reproduce your issue. I use following code and text-block has correct output:

<ProgressBar Extensions:CustomExtensions.Radius="80" Name="test"/>
<TextBlock Text="{Binding ElementName=test, Path=(Extensions:CustomExtensions.Radius)}"/>

Upvotes: 1

Related Questions