Custon Control Error Message

RogerSchlueter-7899 1,256 Reputation points
2023-10-13T13:23:40.9366667+00:00

Here is the Custom Control:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:RSComboBox">
    <Style
        TargetType="{x:Type local:CustomComboBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate
                    TargetType="{x:Type local:CustomComboBox}">
                    <ComboBox 
                        Height="23"
                        ItemsSource="{Binding Source=SourceItems}"
                        SelectedIndex="{Binding Path=SelectedInteger}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
Public Class CustomComboBox
    Inherits Control
     ....
    Public Property SourceItems As List(Of Integer)
    Public Property SelectedInteger As Integer
End Class

Here is the Project to test it:

<Window
    xmlns:rs="clr-namespace:RSComboBox;assembly=RSComboBox"
    ....
    <rs:CustomComboBox
        SelectedInteger="{Binding Path=MyInteger}"
        SourceItems="{Binding Path=MyList}" />
Window>
Class MainWindow
    Public Sub New(
        InitializeComponent()
        DataContext = Me
    End Sub

    Public ReadOnly Property MyList As List(Of Integer)
        Get
            Return New List(Of Integer) From {1, 2, 3, 4, 5}
        End Get
    End Property

    Private _MyInteger As Integer
    Public Property MyInteger As Integer
        Get
            Return _MyInteger
        End Get
        Set(value As Integer)
            _MyInteger = value
        End Set
     End Property
End Class

When I run the project the ComboBox is not populated properly and generates this error message:

Error: 40 : BindingExpression path error: 'SelectedInteger' property not found on 'object' ''MainWindow' (Name='')'. BindingExpression:Path=SelectedInteger; DataItem='MainWindow' (Name=''); target element is 'ComboBox' (Name=''); target property is 'SelectedIndex' (type 'Int32')

I don't know how to fix this error.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,668 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 47,341 Reputation points Microsoft Vendor
    2023-10-13T15:02:12.12+00:00

    Hi,@ Roger Schlueter. Welcome to Microsoft Q&A. You could set SourceItems and SelectedInteger to Dependency Property. And update MainWindow.vb as follows. MainWindow.xaml.vb:

    Imports System.ComponentModel
    
    Public Class MainWindow
        Implements INotifyPropertyChanged
    
        Public Sub New()
            InitializeComponent()
            DataContext = Me
        End Sub
    
        Public ReadOnly Property MyList As List(Of Integer)
            Get
                Return New List(Of Integer) From {1, 2, 3, 4, 5}
            End Get
        End Property
    
        Private _myInteger As Integer
        Public Property MyInteger As Integer
            Get
                Return _myInteger
            End Get
            Set(value As Integer)
                If _myInteger <> value Then
                    _myInteger = value
                    OnPropertyChanged("MyInteger") ' Notify the UI that the property value has changed
                End If
            End Set
        End Property
    
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    
        Protected Sub OnPropertyChanged(propertyName As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
        End Sub
    End Class
    
    
    

    CustomComboBox.vb:

    Public Class CustomComboBox
        Inherits Control
        Public Shared ReadOnly SourceItemsProperty As DependencyProperty = DependencyProperty.Register("SourceItems", GetType(List(Of Integer)), GetType(CustomComboBox), New PropertyMetadata(Nothing))
        Public Property SourceItems As List(Of Integer)
            Get
                Return CType(GetValue(SourceItemsProperty), List(Of Integer))
            End Get
            Set(value As List(Of Integer))
                SetValue(SourceItemsProperty, value)
            End Set
        End Property
    
        Public Shared ReadOnly SelectedIntegerProperty As DependencyProperty = DependencyProperty.Register("SelectedInteger", GetType(Integer), GetType(CustomComboBox), New PropertyMetadata(0))
        Public Property SelectedInteger As Integer
            Get
                Return CInt(GetValue(SelectedIntegerProperty))
            End Get
            Set(value As Integer)
                SetValue(SelectedIntegerProperty, value)
            End Set
        End Property
    
        Shared Sub New()
            DefaultStyleKeyProperty.OverrideMetadata(GetType(CustomComboBox), New FrameworkPropertyMetadata(GetType(CustomComboBox)))
        End Sub
    End Class
    
    
    

    If you still have questions, please feel free to let me know. The problem will be better solved if more details are described (steps, screenshots of error messages, and code to reproduce the problem).


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.