Click here to Skip to main content
15,557,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am having no luck finding out what I am doing wrong. I made a user control that has a List<string> for a dependency property. When I data bind to that property, nothing is in the list. I used the same data to bind to a ComboBox and it shows the list just fine.

Here is my dependency property:
public List<string> ValueList
{
  get { return (List<string>)GetValue(ValueListProperty); }
  set { SetValue(ValueListProperty, value); }
}

// Using a DependencyProperty as the backing store for ValueList.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty ValueListProperty =
    DependencyProperty.Register("ValueList", typeof(List<string>), 
                                             typeof(ValueBox),
                                             new FrameworkPropertyMetadata
                                             {
                                               DefaultValue = new List<string>(),
                                               DefaultUpdateSourceTrigger = System.Windows.Data.UpdateSourceTrigger.PropertyChanged,
                                             });


Here is my data object that I am trying to bind:
private ObservableCollection<string> _valueList1 = new ObservableCollection<string>();
/// <summary>
/// List of items for Valueboxes that display a list of items.
/// </summary>
public ObservableCollection<string> ValueList1
{
  get
  {
    return _valueList1;
  }
  set
  {
    if (value != _valueList1)
    {
      _valueList1 = value;
      NotifyPropertyChanged("ValueList1");
    }
  }
}
It gets populated in the code behind with several values, which I noticed does not fire the Set, but it works with the combobox so I assume that is not the problem.

Here is the XAML for the control. The ComboBox works, my control does not see anything in the ValueList property:
<ComboBox Width="120" 
                       ItemsSource="{Binding ValueList1}"
                       Text="{Binding Label1}"
                       IsEditable="True"/>
<uc:UserControl Value="Test" 
                             ValueType="OPTION"
                             ValueList="{Binding ValueList1}"/>


Now if I put the data in the control in the XAML, it works fine:
<uc:UserControl Value="Yes" 
                             ValueType="OPTION">
    <uc:UserControl.ValueList>
        <System:String>No</System:String>
        <System:String>Yes</System:String>
    </uc:UserControl.ValueList>
</uc:UserControl>
Posted

1 solution

There is a mismatch between the dependency property (List<string>) type and the data object (ObservableCollection<string>).

Still not sure why the combobox was ok with this and my control wasn't unless there is a built-in converter for the combobox.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900