Yiyuan Lee
Yiyuan Lee

Reputation: 679

Attached Property - "Cannot find the Style Property .. on the type .."

I'm currently trying to define an attached property consumable by objects within a usercontrol.

The attached property is registered in the code-behind of said control:

public partial class MapEditor : UserControl
{
    public static readonly DependencyProperty LatitudeProperty =
        DependencyProperty.RegisterAttached("Latitude", typeof(double), typeof(MapEditor));

    //...
 }

I then attempt to bind to these properties in an itemscontrol:

<UserControl xmlns:Controls="clr-namespace:TestProject.Controls" 
             ...>
    <Grid>
         ...
         <ItemsControl ItemsSource={Binding Items}>
             <ItemsControl.ItemContainerStyle>
                <Style TargetType="ContentPresenter">
                    <Setter Property="Controls:MapEditor.Latitude"
                            Value="1" />
                </Style>
            </ItemsControl.ItemContainerStyle>
         </ItemsControl>
    </Grid>
</UserControl>

However I am presented with the following error:

Error   6   Cannot find the Style Property 'Latitude' on the type 'TestProject.Controls.PhaseEditor'

Is there any solution to this? I tried defining the attached properties in a separate class file altogether but the same problem still persists.

Upvotes: 1

Views: 1598

Answers (1)

Yiyuan Lee
Yiyuan Lee

Reputation: 679

I have found the answer to this problem.

It seems that it was necessary to declare public static getters and setters for the dependency properties. That is,

public static double GetLatitude(DependencyObject obj);
public static void SetLatitude(DependencyObject obj, double value);

I believe this need is mentioned somewhere inside the documentation, although I humbly think that it is weird for such a necessity to not be immediately obvious to a user.

Upvotes: 6

Related Questions