wiz*_*rdz 5 silverlight dependency-properties mvvm attached-properties silverlight-4.0
我正在尝试做一些示例应用程序以在 DataGrid 中使用依赖属性,但是当我尝试运行应用程序时,我遇到了运行时异常
在“CustomDependencyProperty”类型中找不到可附加属性“SelectedColumnIndex”。[行: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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的 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>
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚到底是什么问题。我声明依赖属性的方式有什么问题吗?
请帮忙。
谢谢,亚历克斯
我认为你在这里需要一个附加的财产。尝试改变
DependencyProperty.Register
到
DependencyProperty.RegisterAttached.
还有,typeof(object) should be typeof(int)。
更新
是的,以上内容将解决您的问题,但我认为您在这里并不真正需要附加属性,因为您的课程正在扩展DataGrid课程。一个普通的依赖属性就是你所需要的。所以保留你现有的代码并改变
typeof(object),typeof(DataGrid),
Run Code Online (Sandbox Code Playgroud)
到
typeof(int),typeof(CustomDependencyProperty),
Run Code Online (Sandbox Code Playgroud)
在你的 xaml 中,你可以直接使用这个扩展类,就像这样,
<local:CustomDependencyProperty SelectedColumnIndex="{Binding Index,Mode=TwoWay}">
Run Code Online (Sandbox Code Playgroud)
您可能希望将名称“CustomDependencyProperty”更改为更有意义的名称,例如ExtendedDataGrid.
所以我认为结论是您通常有两种创建可绑定属性的方法,一种是扩展控件并创建普通依赖属性,另一种是创建带有附加属性的静态类。
希望这可以帮助。:)
| 归档时间: |
|
| 查看次数: |
17657 次 |
| 最近记录: |