user1956901
user1956901

Reputation: 81

WPF - Selected value of combo box inside user control is not binding

I have reusable user control that contains a textblock and a combo box as shown below:

<UserControl ....>
        <TextBlock DockPanel.Dock="Left" x:Name="label">Title:/TextBlock>
        <ComboBox x:Name="comboBox" ></ComboBox>
</UserControl>

I have created the following dependency properties in the code behind of this control.

public partial class ctlCombobox : UserControl
    {

    public static readonly DependencyProperty DisplayMemberPathProperty = DependencyProperty.Register("DisplayMemberPath", typeof(string), typeof(ctlCombobox), new FrameworkPropertyMetadata() { BindsTwoWayByDefault = true }); 
    public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(ctlCombobox), new FrameworkPropertyMetadata() { BindsTwoWayByDefault = true });          
    public static readonly DependencyProperty SelectedIndexProperty = DependencyProperty.Register("SelectedIndex", typeof(int), typeof(ctlCombobox), new FrameworkPropertyMetadata() { BindsTwoWayByDefault = true });
    public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(ctlCombobox), new FrameworkPropertyMetadata() { BindsTwoWayByDefault = true });
    public static readonly DependencyProperty SelectedValuePathProperty = DependencyProperty.Register("SelectedValuePath", typeof(string), typeof(ctlCombobox), new FrameworkPropertyMetadata() { BindsTwoWayByDefault = true });
    public static readonly DependencyProperty SelectedValueProperty = DependencyProperty.Register("SelectedValue", typeof(object), typeof(ctlCombobox), new FrameworkPropertyMetadata(OnSelectedValueChanged) { BindsTwoWayByDefault = true });


    public ctlCombobox()

    {
        InitializeComponent();

        Binding selectedIndexBinding = new Binding("SelectedIndex") { Source = this, Mode = BindingMode.TwoWay };
        Binding itemsSourceItemBinding = new Binding("ItemsSource") { Source = this, Mode = BindingMode.TwoWay };
        Binding displayMemberPathBinding = new Binding("DisplayMemberPath") { Source = this, Mode = BindingMode.OneWay };
        Binding selectedItemBinding = new Binding("SelectedItem") { Source = this, Mode = BindingMode.TwoWay };
        Binding selectedValueBinding = new Binding("SelectedValue") { Source = this, Mode = BindingMode.TwoWay };
        Binding selectedValuePathBinding = new Binding("SelectedValuePath") { Source = this, Mode = BindingMode.TwoWay };

        comboBox.SetBinding(ComboBox.SelectedIndexProperty, selectedIndexBinding);
        comboBox.SetBinding(ComboBox.ItemsSourceProperty, itemsSourceItemBinding);
        comboBox.SetBinding(ComboBox.DisplayMemberPathProperty, displayMemberPathBinding);
        comboBox.SetBinding(ComboBox.SelectedItemProperty, selectedItemBinding);
        comboBox.SetBinding(ComboBox.SelectedValueProperty, selectedValueBinding);
        comboBox.SetBinding(ComboBox.SelectedValuePathProperty, selectedValuePathBinding);
    }


    public IEnumerable ItemsSource
    {
        get { return (IEnumerable)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    [Browsable(true)]
    public int SelectedIndex
    {
        get { return (int)GetValue(SelectedIndexProperty); }
        set { SetValue(SelectedIndexProperty, value); }
    }

    [Browsable(true)]
    public object SelectedItem
    {
        get { return (object)GetValue(SelectedItemProperty); }
        set { SetValue(SelectedItemProperty, value); }
    }

    [Browsable(true)]
    public object SelectedValue
    {
        get { return (object)GetValue(SelectedValueProperty); }
        set { SetValue(SelectedValueProperty, value); }
    }

    [Browsable(true)]
    public string SelectedValuePath
    {
        get { return (string)GetValue(SelectedValuePathProperty); }
        set { SetValue(SelectedValuePathProperty, value); }
    }

}

I make reference to the control above in my main control as shown below:

<UserControl Name="mainControl" ...>   
    <DataGrid Name="lvEmployee" ItemsSource="{Binding Path=Employees, Mode=TwoWay}"  
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>                    
               <cc:ctlCombobox x:Name="cmbEmployeeType" 
                 ItemsSource="{Binding Source={x:Reference mainControl}, Path=DataContext.EmployeeTypes}"          
                 DisplayMemberPath="Name" SelectedValuePath="Id"   SelectedValue="{Binding   
                 Path=EmployeeTypeId}"  ">
               </cc:ctlCombobox>
         </DataTemplate>
        </DataGrid.RowDetailsTemplate>
  </DataGrid>

I am using the MVVM pattern. I am trying to achieve cmbEmployeeType to be a look up of all possible employee types. Also when an employee is displayed then I want the the cmbEmployeeType to have the appropriate employee type selected. Currently, when load the main Control, the cmbEmployeeType combo box is populated but the nothing is selected in the combo box. I have bounded the combo box to the Employee's employee type id. The user control that contains textboxes that display employee first and last name works fine. There is no binding error in the output. Additionally, there is no issue when I use a plain combo box rather than using my combo box inside user control. Thanks for the response.

Upvotes: 1

Views: 4336

Answers (1)

user1956901
user1956901

Reputation: 81

I had problems figuring this one out but a solution can be found here

Upvotes: 2

Related Questions