Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 679
wizzardz Avatar asked Nov 20 '11 07:11

wizzardz


2 Answers

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. :)

like image 68
Justin XL Avatar answered Sep 28 '22 06:09

Justin XL


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> 
like image 21
wizzardz Avatar answered Sep 28 '22 07:09

wizzardz