Ok, so let this binding work ... first, I use an element from your KgGmItems class to bind to a ComboBox . In this class, you have a collection of string values โโto display in the drop-down list and string properties to bind to ComboBox.SelectedItem ... perfect! Now, I assume that you have an instance of this class in the Resources section called KgGmObj ... make it easier to start with:
<ComboBox DataContext="{StaticResource KgGmObj}" ItemsSource="{Binding KgGmList}" SelectedItem="{Binding ValueSelected, Mode=TwoWay}" />
This is all you need to establish the binding between ComboBox and your class. However, it is worth noting that when you try to install the selected element from your code, it will work only if you set it to one of the actual elements in the collection ... I think that this does not really count when using string s, but itโs important to know this anyway. If instead you configured your own class as an object type in ComboBox , you can set the selected item as follows:
ValueSelected = KgGmList.Where(item => item.Name == "NameOfObjectToMatch").Single();
Or better, if you have a uniquely identifiable property:
ValueSelected = KgGmList.Where(item => item.Id == Id).Single()
With your string values, you should set the selected item from the code as follows:
ValueSelected = "Some value";
UPDATE โ> Okay, so let it go again ... I think I can have enough information to continue. I think you want something like this:
<ComboBox DataContext="{StaticResource KgGmObj}" ItemsSource="{Binding KgGmList}" SelectedItem="{Binding RockDensity_Unit, Mode=TwoWay}" />
The problem is that you installed the DataContext from ComboBox into your KgGmObj object. This means that the Framework will try to find a property named RockDensity_Unit in this object. I also see another potential problem in defining this property.
To link UserControl xaml with its code behind, you need to use DependencyProperty . You can find out how to implement them on the "Dependency Properties Overview" page on MSDN. So first, I would recommend implementing your RockDensity_Unit property as DependencyProperty .
Next, we need to find a way for this property from ComboBox to xaml ... we can do this using the RelativeSource binding as follows:
<ComboBox DataContext="{StaticResource KgGmObj}" ItemsSource="{Binding KgGmList}" SelectedItem="{Binding RockDensity_Unit, RelativeSource={RelativeSource Mode= FindAncestor, AncestorType={x:Type ucStep2}}, Mode=TwoWay}" />
Now, if you have DependencyProperty to bind to the SelectedItem property and your UserControl class is ucStep2 , this should work all ... let me know how this happens.
UPDATE 2 โ>
Your mistake is that you need to add an XML namespace at the top of your XAML file ... something like this:
xmlns:YourNamespace="clr-namespace:ApplicationName.FolderNameContainingClass"
Then you use it to reference your class as follows:
...AncestorType={x:Type YourNamespace:ucStep2} ...
Also, in the DependencyProperty declaration, you must specify the type name of your control, not UserControl , so change
Register("RockDensity_Unit", typeof(string), typeof(UserControl),
to
Register("RockDensity_Unit", typeof(string), typeof(NameOfYourUserControl),
Understood ... replace "NameOfYourUserControl" with the actual name of your class, which extends UserControl .