WPF Combobox databinding
-
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= -
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=I was thinking about binding to the ListView but the problem I had was that the user has the option to Add/Edit/Remove (all buttons on the view) a parameter, and I couldn't figure out how to add a new parameter and still have it be bound. Does that make sense? Can I email you offline? I would like to discuss this offline with some screen shots of what I am trying to duplicate.
modified on Friday, March 6, 2009 12:46 PM
-
I was thinking about binding to the ListView but the problem I had was that the user has the option to Add/Edit/Remove (all buttons on the view) a parameter, and I couldn't figure out how to add a new parameter and still have it be bound. Does that make sense? Can I email you offline? I would like to discuss this offline with some screen shots of what I am trying to duplicate.
modified on Friday, March 6, 2009 12:46 PM
-
OK I am going back through now and trying you recommendations and this is the error that I am getting in the Output window System.Windows.Data Error: 39 : BindingExpression path error: 'Name' property not found on 'object' ''ParameterName' (HashCode=62178992)'. BindingExpression:Path=Name.ParameterDisplayName; DataItem='ParameterName' (HashCode=62178992); target element is 'ComboBox' (Name='cboParameterName'); target property is 'NoTarget' (type 'Object')
-
OK I have finally figured it out, but it really is a different work around. Or at least it seems like it. If instead of creating the ParameterName objects with all of the properties associated with it, if I only create a collection of strings and then bind them to the dropdown box then I can make the binding work. But it is not the object itself it is only one piece of the object.
-
OK I have finally figured it out, but it really is a different work around. Or at least it seems like it. If instead of creating the ParameterName objects with all of the properties associated with it, if I only create a collection of strings and then bind them to the dropdown box then I can make the binding work. But it is not the object itself it is only one piece of the object.
I am suprised it does not work while binding to the the string property of the object. Well, if it is so, you can use a converter taking the object and returning a string for it rather than having to have a string collection. Which particular binding was failing, the one in the combobox DataTemplate ? I still feel that your data was not being populated correct.