gpt4 book ai didi

silverlight - 在 Silverlight 中使用依赖属性时出现 "The attachable property not found in type "错误

转载 作者:行者123 更新时间:2023-12-04 03:06:08 25 4
gpt4 key购买 nike

我正在尝试做一些示例应用程序以在 DataGrid 中使用依赖属性,但是当我尝试运行应用程序时,我遇到了运行时异常

The attachable property 'SelectedColumnIndex' was not found in type 'CustomDependencyProperty'. [Line: 17 Position: 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>

我无法弄清楚问题出在哪里。我声明依赖属性的方式有什么问题吗?

请帮忙。

谢谢,
亚历克斯

最佳答案

我想你需要一个 attached property这里。尝试改变
DependencyProperty.Register

DependencyProperty.RegisterAttached .

另外,typeof(object) should be typeof(int) .

更新

是的,以上将解决您的问题,但我认为您在这里并不需要附加属性,因为您的类(class)正在扩展 DataGrid类(class)。您只需要一个正常的依赖属性。所以保留你现有的代码并改变

typeof(object),typeof(DataGrid), 


typeof(int),typeof(CustomDependencyProperty), 

在你的xaml中,你可以直接使用这个扩展类,像这样,
<local:CustomDependencyProperty SelectedColumnIndex="{Binding Index,Mode=TwoWay}">

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

所以我认为结论是您通常有两种创建可绑定(bind)属性的方法,或者通过扩展控件并创建正常的依赖属性,或者通过创建带有附加属性的静态类。

希望这可以帮助。 :)

关于silverlight - 在 Silverlight 中使用依赖属性时出现 "The attachable property not found in type "错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8199997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com