我有一个依赖属性,它是基于文本框的自定义控件的一部分:在wpf 4.5,vb.net 4.5,visual studio 2012中。
这是财产声明:
#Region "DEPENDENCY PROPERTIES -- ItemsSource"
Public Property ItemsSource As IEnumerable
Get
Return GetValue(ItemsSourceProperty)
End Get
Set(ByVal value As IEnumerable)
SetValue(ItemsSourceProperty, value)
End Set
End Property
Public ReadOnly ItemsSourceProperty As DependencyProperty = DependencyProperty.Register( _
"ItemsSource", GetType(DependencyObject), GetType(AutoCompleteTextBox), _
New FrameworkPropertyMetadata(Nothing, FrameworkPropertyMetadataOptions.None, _
New PropertyChangedCallback(AddressOf OnItemSourceChanged)))
#End Region
然后我在一个小样本项目中声明自定义控件进行测试(自定义控件在同一个魂中的另一个项目中)
以下是带有自定义控件的主窗口的xaml:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:krisis="clr-namespace:Krisis.Controls;assembly=Krisis.Controls"
Title="MainWindow" Height="350" Width="525" x:Name="MyWindow"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<krisis:AutoCompleteTextBox ItemsSource="{Binding Collection, Mode=TwoWay}" Width="497" MinHeight="35" FontSize="18" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,41,10,243"/>
</Grid>
</Window>
但是xaml编辑器强调了customcontrol行并抛出以下错误:
错误1无法在'ItemsSource'属性上设置'绑定' 输入'AutoCompleteTextBox'。 '绑定'只能在a上设置 DependencyObject的DependencyProperty。
有人可以帮我解决导致此错误的原因,我无法弄清楚我的依赖属性声明在哪里出错。
答案 0 :(得分:1)
DependencyProperty
Shared
或VB
中的Static
C#
必须是Public Property ItemsSource As IEnumerable
Get
Return GetValue(ItemsSourceProperty)
End Get
Set(ByVal value As IEnumerable)
SetValue(ItemsSourceProperty, value)
End Set
End Property
Public Shared ReadOnly ItemsSourceProperty As DependencyProperty = DependencyProperty.Register( _
"ItemsSource", GetType(DependencyObject), GetType(AutoCompleteTextBox), _
New FrameworkPropertyMetadata(Nothing, FrameworkPropertyMetadataOptions.None, _
New PropertyChangedCallback(AddressOf OnItemSourceChanged)))
示例:
{{1}}