&引用;在类型“中找不到可附加属性”;在Silverlight中使用依赖项属性时出错

&引用;在类型“中找不到可附加属性”;在Silverlight中使用依赖项属性时出错,silverlight,silverlight-4.0,mvvm,dependency-properties,attached-properties,Silverlight,Silverlight 4.0,Mvvm,Dependency Properties,Attached Properties,我尝试在DataGrid中使用Dependency属性,但当我尝试运行应用程序时,我得到了一个运行时异常 在类型中找不到可附加属性“SelectedColumnIndex” “CustomDependencyProperty”。[第17行位置:74] 这是我用来声明依赖项属性的代码 public class CustomDependencyProperty : DataGrid { public static DependencyProperty SelectedColumnIndex

我尝试在DataGrid中使用Dependency属性,但当我尝试运行应用程序时,我得到了一个运行时异常

在类型中找不到可附加属性“SelectedColumnIndex” “CustomDependencyProperty”。[第17行位置:74]

这是我用来声明依赖项属性的代码

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);
        }
    }
}
这是我的XAML代码

<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>

我无法找出问题所在。我声明依赖项属性的方式是否有任何错误

请帮忙

谢谢, 亚历克斯

我想你在这里需要一份工作。试着改变

dependencProperty.Register

DependencyProperty.RegisterAttached

另外,
typeof(object)应该是typeof(int)

更新

是的,上面的内容可以解决您的问题,但是我认为您不需要在这里附加属性,因为您的类正在扩展
DataGrid
类。您只需要一个普通的依赖属性。因此,保留现有代码并进行更改

typeof(object),typeof(DataGrid), 

在您的xaml中,您可以直接使用这个扩展类

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

您可能希望将名称“CustomDependencyProperty”更改为更有意义的名称,如
ExtendedDataGrid

因此,我认为结论是,通常有两种方法可以创建可绑定属性,一种是扩展控件并创建普通依赖属性,另一种是创建带有附加属性的静态类


希望这有帮助。:)

我想我现在可以回答这个问题了。这个例外只是解释了
AttachedProperty
DependencyProperty
之间的区别

要使用依赖属性SelectedColumnIndex,我应该像这样重新定义我的
DataGrid
xaml

<local:CustomDependencyProperty x:Name="customGrid" 
                                 AutoGenerateColumns="True" 
                                 ItemsSource="{Binding Responses}" 
                                 SelectedColumnIndex="{Binding Index, Mode=TwoWay}">
</local:CustomDependencyProperty> 


确实是的,最初它是一个附加属性。我将其更改为依赖属性,以查看依赖属性和附加属性之间的区别。我想现在我知道哪里出了问题:)我已更新了答案,希望它能为您澄清ap和dp的区别:将此标记为“答案”,因为它有更好的解释:)谢谢!你的答案也是正确的。:)
<local:CustomDependencyProperty x:Name="customGrid" 
                                 AutoGenerateColumns="True" 
                                 ItemsSource="{Binding Responses}" 
                                 SelectedColumnIndex="{Binding Index, Mode=TwoWay}">
</local:CustomDependencyProperty>