1
votes

I have a dependency property that is part of a custom control based on a textbox: in wpf 4.5, vb.net 4.5, visual studio 2012.

Here is the property declaration:

#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

I then declare the custom control in a small sample project for testing (the custom control is inside another project in the same soultion)

Here is the xaml for the main window with the custom control:

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

But the xaml editor underlines the customcontrol line and throws the following error:

Error 1 A 'Binding' cannot be set on the 'ItemsSource' property of type 'AutoCompleteTextBox'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

Can someone help me resolve what is causing this error, I can't figure out where my dependency property declaration is wrong.

1
Post the full code of the control. Also, why is your property declared as typeof(DependencyObject)? it should be typeof(IEnumerable).Federico Berasategui
@HighCore your right, I was playing around trying to get things to work.J King

1 Answers

2
votes

The DependencyProperty must be Shared in VB or Static in C#

Example:

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