kristofdielis
Programmer
- Sep 1, 2011
- 25
Hi everyone,
I have several controls on a WPF user control, most of these are comboboxes. Behind each control, there is a button, that will open up a new window, which will return a user selected value.
Consider, one of these comboboxes contains a list of sites:
These sites are defined like this:
And the combobox like this:
This depends on a ViewModel which contains the Hierarchy property (object), which contains both the Sites and SiteId. SiteId is used only so you have a binding point to work with (which is also the Combobox's SelectedValue.
The button behind the Sites combobox is defined like this:
Now, Hierarchy.SiteId is a string. It is passed as a parameter to the receiving method (through RelayCommand), which handles it as an object.
And here's the problem:
Even though the parameter has TwoWay binding, any changes to the value, are not returned. But, if I pass the entire object, say a TextBox, and set the .text value, than everything is alright.
But, preferably, I'd rather not have to do it like this, because I can expect any sort of object, and I'd have to build a fairly elaborate type check, to know what I am supposed to be doing with it. And, in case of the comboboxes, it's not exactly easy, because I have multiple items to deal with. Sure, I could use the SelectedItem property, but then I'd have to type check multiple types on top of the ComboBox type check.
The above is well, a possible solution, but getting the string binding to properly work, would be far more elegant.
Any suggestions?
I have several controls on a WPF user control, most of these are comboboxes. Behind each control, there is a button, that will open up a new window, which will return a user selected value.
Consider, one of these comboboxes contains a list of sites:
These sites are defined like this:
Code:
public ObservableCollection<Site> Sites { get; private set; }
And the combobox like this:
Code:
<telerik:RadComboBox Name="cbxSite"
ItemsSource="{Binding Hierarchy.Sites}"
SelectedValue="{Binding Hierarchy.SiteId, Mode=TwoWay}"
DisplayMemberPath="Name"
SelectedValuePath="site_id"
/>
This depends on a ViewModel which contains the Hierarchy property (object), which contains both the Sites and SiteId. SiteId is used only so you have a binding point to work with (which is also the Combobox's SelectedValue.
The button behind the Sites combobox is defined like this:
Code:
<tlrk:RadButton Name="btnSiteIdConstructValue">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cmd:EventToCommand PassEventArgsToCommand="False"
Command="{Binding ConstructValueCommand}"
CommandParameter="{Binding Hierarchy.SiteId}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Image Source="someImage.png" />
</tlrk:RadButton>
Now, Hierarchy.SiteId is a string. It is passed as a parameter to the receiving method (through RelayCommand), which handles it as an object.
And here's the problem:
Even though the parameter has TwoWay binding, any changes to the value, are not returned. But, if I pass the entire object, say a TextBox, and set the .text value, than everything is alright.
But, preferably, I'd rather not have to do it like this, because I can expect any sort of object, and I'd have to build a fairly elaborate type check, to know what I am supposed to be doing with it. And, in case of the comboboxes, it's not exactly easy, because I have multiple items to deal with. Sure, I could use the SelectedItem property, but then I'd have to type check multiple types on top of the ComboBox type check.
The above is well, a possible solution, but getting the string binding to properly work, would be far more elegant.
Any suggestions?