WPF Combobox databinding
-
I have a combobox which loads and displays exactly how I want it to, it is in a view and I am able to retrieve the pieces from this view without any problem. The problem I have is when I throw the object back at the view the combobox(which is bound to a collection of objects) the combobox doesn't show the information. What am I missing? Here is the combobox XAML:
I think that you are missing the ElementName in the ItemsSource Binding. In this case the ElementName is the x:Name of your window or UserControl. IsSynchronizedWithCurrentItem="True" TabIndex="10" Grid.Column="1" Grid.ColumnSpan="4" ItemsSource="{Binding Path=ParameterNameCollection, ElementName=UserControl, Mode=Default}" SelectedValue="{Binding SelectedParameter.Name}" Style="{DynamicResource BaseComboBox}">
-
I think that you are missing the ElementName in the ItemsSource Binding. In this case the ElementName is the x:Name of your window or UserControl. IsSynchronizedWithCurrentItem="True" TabIndex="10" Grid.Column="1" Grid.ColumnSpan="4" ItemsSource="{Binding Path=ParameterNameCollection, ElementName=UserControl, Mode=Default}" SelectedValue="{Binding SelectedParameter.Name}" Style="{DynamicResource BaseComboBox}">
-
-
I have a combobox which loads and displays exactly how I want it to, it is in a view and I am able to retrieve the pieces from this view without any problem. The problem I have is when I throw the object back at the view the combobox(which is bound to a collection of objects) the combobox doesn't show the information. What am I missing? Here is the combobox XAML:
-
-
Can you share how you are updating the object in code ? Try calling the property change notification explicitly using SendPropertyChanged("ParameterNameCollection").
There are four different view models since there is are parent child relationships in this scenario. Here is an attempt to explain TabParameterViewModel has a DP of ParameterCollection (which is of Type ObservableCollection) has a DP of Parameter (bound to the selected item in a ListView) ParameterViewModel has a DP of Parameter which is set to a clone of TabParameterViewModel.Parameter has a DP of ParameterDefaultCollection (again an ObservableCollection) this.Parameter.Defaults ParameterDefaultsViewModel has a DP of ParameterDefaultCollection bound to a ListView has a DP of ParameterDefault bound to the selectedItem in the ListView ParameterDefaultViewModel has a DP of ParameterDefault which is set to a clone of ParameterDefaultsViewModel.ParameterDefault The Parameter Model has a Name property which is a class of ParameterName, which contains other attributes that are needed for the ParameterName. It also has a string property called ParameterLabel which I have bound to a textbox, when I change the SelectedItem in the listbox int TabParameterView the corresponding attributes change in the ParameterView, but any item bound to an object (in this case a Combobox) that attribute is not populated.
-
There are four different view models since there is are parent child relationships in this scenario. Here is an attempt to explain TabParameterViewModel has a DP of ParameterCollection (which is of Type ObservableCollection) has a DP of Parameter (bound to the selected item in a ListView) ParameterViewModel has a DP of Parameter which is set to a clone of TabParameterViewModel.Parameter has a DP of ParameterDefaultCollection (again an ObservableCollection) this.Parameter.Defaults ParameterDefaultsViewModel has a DP of ParameterDefaultCollection bound to a ListView has a DP of ParameterDefault bound to the selectedItem in the ListView ParameterDefaultViewModel has a DP of ParameterDefault which is set to a clone of ParameterDefaultsViewModel.ParameterDefault The Parameter Model has a Name property which is a class of ParameterName, which contains other attributes that are needed for the ParameterName. It also has a string property called ParameterLabel which I have bound to a textbox, when I change the SelectedItem in the listbox int TabParameterView the corresponding attributes change in the ParameterView, but any item bound to an object (in this case a Combobox) that attribute is not populated.
-
Your orignal post had the ComboBox bound to "ParameterNameCollection" which doesn't appear in your description. What collection is it ?
The ParameterViewModel has an ObservableCollection ParameterNameCollection which is a collection of possible names that it could be, with attributes associated to that name, so the Collection is what fills the combobox, but I want the SelectedItem to be the ParameterName of the Parameter object. If I was to post the complete class for the ParameterViewModel it would look like this: ParameterViewModel has a ControlModeCollection (ObservableCollection) has a ControlTypeCollection (ObservableCollection) has a DataTypeCollection (ObservableCollection) has a ParameterMethodCollection (ObservableCollection) has a ParameterNameCollection (ObservableCollection) has a DP of SelectedParameter (of Type Parameter) has a DP of SelectedParameterDefaults (I think this is redundant since it is an ObservableCollection) Then the Parameter class looks like the following has a Name (of Type ParameterName) has a DataType (of Type DataType) has a ScreenLabel (of Type String) etc. etc. the TextBox XAML works as expected, the ComboBox doesn't
-
There are four different view models since there is are parent child relationships in this scenario. Here is an attempt to explain TabParameterViewModel has a DP of ParameterCollection (which is of Type ObservableCollection) has a DP of Parameter (bound to the selected item in a ListView) ParameterViewModel has a DP of Parameter which is set to a clone of TabParameterViewModel.Parameter has a DP of ParameterDefaultCollection (again an ObservableCollection) this.Parameter.Defaults ParameterDefaultsViewModel has a DP of ParameterDefaultCollection bound to a ListView has a DP of ParameterDefault bound to the selectedItem in the ListView ParameterDefaultViewModel has a DP of ParameterDefault which is set to a clone of ParameterDefaultsViewModel.ParameterDefault The Parameter Model has a Name property which is a class of ParameterName, which contains other attributes that are needed for the ParameterName. It also has a string property called ParameterLabel which I have bound to a textbox, when I change the SelectedItem in the listbox int TabParameterView the corresponding attributes change in the ParameterView, but any item bound to an object (in this case a Combobox) that attribute is not populated.
babongita wrote:
but any item bound to an object (in this case a Combobox) that attribute is not populated
The above statement confuses me. It says any item bound to the object is not populated. You mean never or again just when you do something from code ?
-
The ParameterViewModel has an ObservableCollection ParameterNameCollection which is a collection of possible names that it could be, with attributes associated to that name, so the Collection is what fills the combobox, but I want the SelectedItem to be the ParameterName of the Parameter object. If I was to post the complete class for the ParameterViewModel it would look like this: ParameterViewModel has a ControlModeCollection (ObservableCollection) has a ControlTypeCollection (ObservableCollection) has a DataTypeCollection (ObservableCollection) has a ParameterMethodCollection (ObservableCollection) has a ParameterNameCollection (ObservableCollection) has a DP of SelectedParameter (of Type Parameter) has a DP of SelectedParameterDefaults (I think this is redundant since it is an ObservableCollection) Then the Parameter class looks like the following has a Name (of Type ParameterName) has a DataType (of Type DataType) has a ScreenLabel (of Type String) etc. etc. the TextBox XAML works as expected, the ComboBox doesn't
babongita wrote:
SelectedValue="{Binding SelectedParameter.Name, Mode=TwoWay}"
and
babongita wrote:
<TextBlock Text="{Binding ParameterDisplayName}"/>
I guess both of these are bound to the Parameter object. Was just curious to know if these are two different properties or a typo ? Cause your textbox is bound to a property names "ScreenLabel", which I think is the display label.
babongita wrote:
Then the Parameter class looks like the following has a Name (of Type ParameterName)
In your description, you say, Name is of type "ParameterName". So then, I suspect this,
SelectedValue="{Binding SelectedParameter.Name, Mode=TwoWay}"
Here you are binding to SelectedParameter.Name where Name is of type ParamterName. The binding will fail since it is not binding to a string type. If you open your output window in the solution explorer (assuming you are using VS) you would be able to see binding failure messages logged.
-
babongita wrote:
but any item bound to an object (in this case a Combobox) that attribute is not populated
The above statement confuses me. It says any item bound to the object is not populated. You mean never or again just when you do something from code ?
OK, There is a listbox below, which is bound to Collection of Parameters(this is a class, not what you would typically think of when you think parameters), then there is a "view" above it which shows the "detail" (attributes) of the SelectedItem in the listbox. All of the TextBoxes or the CheckBoxes show the "detail" (attributes) correctly. But when the "detail" (attribute) is an item in a Combobox the item doesn't show in the Combobox even though it is bound to that "detail"(attribute). The combobox is populated with all of the possible selections, but the SelectedItem, isn't shown in that combobox.
-
babongita wrote:
SelectedValue="{Binding SelectedParameter.Name, Mode=TwoWay}"
and
babongita wrote:
<TextBlock Text="{Binding ParameterDisplayName}"/>
I guess both of these are bound to the Parameter object. Was just curious to know if these are two different properties or a typo ? Cause your textbox is bound to a property names "ScreenLabel", which I think is the display label.
babongita wrote:
Then the Parameter class looks like the following has a Name (of Type ParameterName)
In your description, you say, Name is of type "ParameterName". So then, I suspect this,
SelectedValue="{Binding SelectedParameter.Name, Mode=TwoWay}"
Here you are binding to SelectedParameter.Name where Name is of type ParamterName. The binding will fail since it is not binding to a string type. If you open your output window in the solution explorer (assuming you are using VS) you would be able to see binding failure messages logged.
First of all you are correct they are bound to different properties of the Parameter object. My question is though, that I am binding the ComboBox to a Collection of Classes, and the Parameter object as a property which is a the same class, so I would think that the SelectedValue should be able to bind directly to that Value, since they are of the same type. It is just instead of it being a string object, is of another type. I tried using: SelectedValue="{Binding SelectedParameter.Name.ParameterDisplayName, Mode=TwoWay}" but then the actual property doesn't get set correctly. How can I set that combobox to be the correct item then?
-
First of all you are correct they are bound to different properties of the Parameter object. My question is though, that I am binding the ComboBox to a Collection of Classes, and the Parameter object as a property which is a the same class, so I would think that the SelectedValue should be able to bind directly to that Value, since they are of the same type. It is just instead of it being a string object, is of another type. I tried using: SelectedValue="{Binding SelectedParameter.Name.ParameterDisplayName, Mode=TwoWay}" but then the actual property doesn't get set correctly. How can I set that combobox to be the correct item then?
Give this a try,
<ComboBox x:Name="cboParameterName"
VerticalAlignment="Center"
IsSynchronizedWithCurrentItem="True"
TabIndex="10"
Grid.Column="1"
Grid.ColumnSpan="4"
ItemsSource="{Binding Path=ParameterNameCollection}"
SelectedValue="{Binding Path=SelectedParameter.Name.ParameterDisplayName}"
SelectedValuePath="Name.ParameterDisplayName"
Style="{DynamicResource BaseComboBox}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name.ParameterDisplayName}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>Note: Added SelectedValuePath and changed the DataTemplate (fingers crossed ,X,, )
-
Give this a try,
<ComboBox x:Name="cboParameterName"
VerticalAlignment="Center"
IsSynchronizedWithCurrentItem="True"
TabIndex="10"
Grid.Column="1"
Grid.ColumnSpan="4"
ItemsSource="{Binding Path=ParameterNameCollection}"
SelectedValue="{Binding Path=SelectedParameter.Name.ParameterDisplayName}"
SelectedValuePath="Name.ParameterDisplayName"
Style="{DynamicResource BaseComboBox}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name.ParameterDisplayName}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>Note: Added SelectedValuePath and changed the DataTemplate (fingers crossed ,X,, )
I have been playing with using the SelectedIndex. And it seems to work, at least for now, the problem that I foresee is that I will have to go through the Combobox source to get the index each time, in the event that the Index would change, either by addition or substraction of Metadata. What are your thoughts on that?
-
Give this a try,
<ComboBox x:Name="cboParameterName"
VerticalAlignment="Center"
IsSynchronizedWithCurrentItem="True"
TabIndex="10"
Grid.Column="1"
Grid.ColumnSpan="4"
ItemsSource="{Binding Path=ParameterNameCollection}"
SelectedValue="{Binding Path=SelectedParameter.Name.ParameterDisplayName}"
SelectedValuePath="Name.ParameterDisplayName"
Style="{DynamicResource BaseComboBox}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name.ParameterDisplayName}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>Note: Added SelectedValuePath and changed the DataTemplate (fingers crossed ,X,, )
BTW, I tried your suggestion and the first problem was that the combobox wasn't showing what was loaded, the second problem was that the parameter.name wasn't being set either
-
BTW, I tried your suggestion and the first problem was that the combobox wasn't showing what was loaded, the second problem was that the parameter.name wasn't being set either
babongita wrote:
combobox wasn't showing what was loaded
What did it show then ? Was there any value in the SelectedParameter, since that is what is driving the SelectedItem.
babongita wrote:
parameter.name wasn't being set either
Why is this a combobox problem ?? Combobox is reading this value, someone has to be setting it ?? Did you check your output window? It would be logging DataBinding errors.
-
babongita wrote:
combobox wasn't showing what was loaded
What did it show then ? Was there any value in the SelectedParameter, since that is what is driving the SelectedItem.
babongita wrote:
parameter.name wasn't being set either
Why is this a combobox problem ?? Combobox is reading this value, someone has to be setting it ?? Did you check your output window? It would be logging DataBinding errors.
I didn't check the output window to see what was happening. When I hit the dropdown on the combo I couldn't see anything in it, but the itemsource had the values. But digging into the SelectedIndex seems to be the way to go here, it is performing as expected. But like I said, I will need to get the Index each time the screen loads, since the Index may change, not often but enough that I can't rely on that being the same I guess I could create a converter for that couldn't I, then it would be more of an "automated" thing, right?
-
I didn't check the output window to see what was happening. When I hit the dropdown on the combo I couldn't see anything in it, but the itemsource had the values. But digging into the SelectedIndex seems to be the way to go here, it is performing as expected. But like I said, I will need to get the Index each time the screen loads, since the Index may change, not often but enough that I can't rely on that being the same I guess I could create a converter for that couldn't I, then it would be more of an "automated" thing, right?
babongita wrote:
I didn't check the output window to see what was happening. When I hit the dropdown on the combo I couldn't see anything in it, but the itemsource had the values.
That is because some binding is failing. If you check the Output window it would clearly mention which binding is failing and why. The comboox SelectedItem is being driven by the SelectedParamter Dependency Property. Check what value it has. If it is null(or the Name property), then the combobox cannot do anything.
babongita wrote:
But digging into the SelectedIndex seems to be the way to go here, it is performing as expected. But like I said, I will need to get the Index each time the screen loads, since the Index may change, not often but enough that I can't rely on that being the same
So, if I understand you correct, you will search for the objects in the comboxbox and set the combobox SelectedIndex to it ? Would this be on the SelectionChange of your listviewm - where you would get the listview's selected item and search that in the combobox ?
babongita wrote:
I could create a converter
Well, that seems to be a *workaround*. I would fix the damn databinding issue :) I think you could look at this as an option,
<ComboBox x:Name="cboParameterName"
VerticalAlignment="Center"
IsSynchronizedWithCurrentItem="True"
TabIndex="10"
Grid.Column="1"
Grid.ColumnSpan="4"
ItemsSource="{Binding Path=ParameterNameCollection}"
SelectedItem="{Binding Path=SelectedParameter}"
Style="{DynamicResource BaseComboBox}"
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name.ParameterDisplayName}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>One more thing is, since you are driving this combobox from a listview, why don't you bind the ListView's SelectedItem to the combobox's SelectedItem ,
<ComboBox x:Name="cboParameterName"
VerticalAlignment="Center"
IsSynchronizedWithCurrentItem=