Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TwoWay binding on CommandParameter works on objects, but not on string 1

Status
Not open for further replies.

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:
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?
 
this may have to do with the object type. strings are immutable, where POCO objects, by default, are not. so if a string is changed, technically a new instance is created a returned, but the original value remains the same.

with a custom POCO object you can change the value of the referenced properties, therefore that instanced changed. it now references a new string.

rather than bind a specific type to the drop down you could convert your custom types to a common UI object. I believe there is even an Linq Extension for this
Code:
new[]{}.ToLooup(key selector, value selector);
you may also want to take a look at Caliburn.Micro as a framework for MVVM/MVP/MVC etc. The framework is built on the notion of convention over configuration so a lot of the boiler plate, repetitive code is handled for you automatically.


Jason Meckley
Senior Programmer

faq855-7190
faq732-7259
My Blog
 
Hi Jason,

Thanks for the response, to be honest, I didn't really expect one, so you get a star at least for trying. I'll check it out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top