"The attachable property not found in type " error while using a dependency property in Silverlight
Asked Answered
L

2

5

I am trying to do some sample applications to use Dependency Property in a DataGrid,but when i tried to run application I am getting an run time exception

The attachable property 'SelectedColumnIndex' was not found in type 'CustomDependencyProperty'. [Line: 17 Position: 74]

This is the code i used to declare my dependency property

public class CustomDependencyProperty : DataGrid
{

    public static DependencyProperty SelectedColumnIndexProperty = DependencyProperty.Register("SelectedColumnIndex",
                                                                                                 typeof(object),
                                                                                                 typeof(DataGrid),
                                                                                                 new PropertyMetadata(0));

    public int SelectedColumnIndex
    {
        get
        {
            return (int)GetValue(SelectedColumnIndexProperty);
        }

        set
        {
            SetValue(SelectedColumnIndexProperty, value);
        }
    }
}

And this is my XAML code

<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="BindingDictionary.MainPage"
    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"
             xmlns:local="clr-namespace:BindingDictionary"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <local:SimpleConverter x:Key="myConverter"></local:SimpleConverter>
    </UserControl.Resources>
        <Grid x:Name="LayoutRoot" Background="White">
        <sdk:DataGrid x:Name="dataGrid"
                      AutoGenerateColumns="True"
                      ItemsSource="{Binding Responses}" 
                      local:CustomDependencyProperty.SelectedColumnIndex="{Binding Index,Mode=TwoWay}">
        </sdk:DataGrid>
        <TextBlock x:Name="DisplayIndex" Text="{Binding Index}" />
    </Grid>
</UserControl>

I am unable to figure out what excatly is the problem.Is there anything wrong in the way I declare a dependency property?

Please help.

Thanks, Alex

Laminitis answered 20/11, 2011 at 7:1 Comment(0)
Q
6

I think you need an attached property here. Try changing

DependencyProperty.Register

to

DependencyProperty.RegisterAttached.

Also, typeof(object) should be typeof(int).

UPDATE

Yes, the above will fix your problem, but I think you don't really need an attached property here as your class is extending the DataGrid class. A normal dependency property is all you need. So keep your existing code and change

typeof(object),typeof(DataGrid), 

to

typeof(int),typeof(CustomDependencyProperty), 

and in your xaml, you can just use this extended class directly, something like this,

<local:CustomDependencyProperty SelectedColumnIndex="{Binding Index,Mode=TwoWay}">

You might want to change the name 'CustomDependencyProperty' to be something more meanful like ExtendedDataGrid.

So I think the conclusion is you normally have two ways of creating a bindable property, either by extending the control and creating a normal dependency property, or by creating a static class with an attached property.

Hope this helps. :)

Quaker answered 20/11, 2011 at 7:25 Comment(4)
yes indeed, initially it was an attached property.I changed that to a dependency property to see what excatly is the diffrence between an dependency property and an AttachedPropety.I think now I know what went wrong :)Laminitis
I have updated the answer, hopefully it will give you some clarification on the difference of ap and dp. :)Quaker
:marking this a 'answer' as it has got a better explanation :)Laminitis
Thanks! Yours is a valid answer too. :)Quaker
L
2

I think I can answer this question now.This exception just explains what exactly is the diffrence between an AttachedProperty and a DependencyProperty.

To use a dependency property SelectedColumnIndex I should redefine my DataGrid xaml like this

<local:CustomDependencyProperty x:Name="customGrid" 
                                 AutoGenerateColumns="True" 
                                 ItemsSource="{Binding Responses}" 
                                 SelectedColumnIndex="{Binding Index, Mode=TwoWay}">
</local:CustomDependencyProperty> 
Laminitis answered 20/11, 2011 at 7:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.