1

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.

2
  • 1
    Post the full code of the control. Also, why is your property declared as typeof(DependencyObject)? it should be typeof(IEnumerable). Commented Apr 15, 2013 at 20:58
  • @HighCore your right, I was playing around trying to get things to work. Commented Apr 15, 2013 at 21:09

1 Answer 1

2

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)))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, fixed it! This is my first day with dependency properties. If you don't mind, why does it need to be shared?
@J King, Dependency properties are used in binding, animations, etc. The definition of it is created static, because WPF runtime needs to access the definition without having to instantiate the objects multiple times. It was designed like that because instantiating unnecessary object only for getting the dependency property's definition will cost performance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.