Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello all,

I am experiencing some difficulties trying to bind to multiple properties, that exist on my view model. The bindings need to be shown in a comma-separated format within a cell, in a RadGridView (similar to the standard WPF DataGridView).

The properties that I am binding to all live within the same AddressDetails object on the view model. AddressDetails contains the properties: Address1, Address2, Address3.

I have looked into various solutions to the problem but each appear to have their disadvantages. Any help would be most appreciated, I would like to try steer clear of creating a converter, but if it is the only viable solution, then so be it.

Solution 1(Multi-binding):
This solution uses multi-bindings and the StringFormat property, to display the data. However the commas will always show, regardless if the address properties have any data.

XML
<telerik:RadGridView ItemsSource="{Binding Locations, Mode=TwoWay}"
                     SelectedItem="{Binding SelectedLocation, Mode=TwoWay}">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn Header="Reference" DataMemberBinding="{Binding Reference}"/>
        <telerik:GridViewDataColumn Header="Address Summary">
            <telerik:GridViewDataColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock>
                        <TextBlock.Text>
                           <MultiBinding StringFormat=" {0}, {1}, {2} ">
                               <Binding Path="AddressDetails.Address1"/>
                               <Binding Path="AddressDetails.Address2"/>
                               <Binding Path="AddressDetails.Address3"/>
                           </Multibinding>
                        </TextBlock.Text>
                    </TextBlock>
                </DataTemplate>
            </telerik:GridViewDataColumn.CellTemplate>
        </telerik:GridViewDataColumn>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>




Solution 2(Bind to a ToString override):
Essentially all that is happening here is a standard override provided for the AddressDetails object:

C#
public override string ToString()
{
    string result = string.Empty;
    string[] addressSummaryFields = (from a in new string[] { Address1, Address2, Address3, Address4, Address5, Country, PostCode }
                               where !string.IsNullOrEmpty(a)
                               select a).ToArray<string>();

    if (addressSummaryFields.Length > 0)
    {
        result = addressSummaryFields.Aggregate((o, n) => o = o + ", " + n);
    }

    return result;
}


This solution works until the values are changed by an editor control below the grid, as the binding is to the AddressDetails object itself. Which means when the Address1 property changes, only the Address1 property sends out a notification as opposed to the entire AddressDetails object. This means the UI does not update the ToString overriden binding.

XML
<telerik:RadGridView ItemsSource="{Binding Locations, Mode=TwoWay}"
                     SelectedItem="{Binding SelectedLocation, Mode=TwoWay}">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn Header="Reference" 
                                    DataMemberBinding="{Binding Reference}"/>
        <telerik:GridViewDataColumn Header="Address Summary" 
                                    TextWrapping="WrapWithOverflow" 
                                    DataMemberBinding="{Binding AddressDetails}"/>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>


Thanks in advance,
SeniorCrispy.
Posted
Updated 29-May-12 3:16am
v4
Comments
db7uk 29-May-12 9:30am    
On your AddressDetails object, can you not have a readonly property that is exposed. This property could format the address1, address2 address3 values into a string. If address1, 2 or 3 is updated (and obviously throwing OnPropertyChangedEvent) you could also throw the OnPropertyChangedEvent on the readonly property thus firing the change notification you are after?
Walby 29-May-12 10:22am    
I considered doing this, but the issue with this is that the AddressDetails object, inherits from entity in our entity framework and will be used by other developers in other views. So I can't really add a property for the purpose of presentation. Potentially this screen might be the only screen that requires the data in the comma separated format.
db7uk 29-May-12 10:32am    
ok, I see where you are coming from. The only thing then (without converters) is in your ViewModel have a linq statement that builds a new list of objects (or a strongly typed class) from the original data. By doing this you could populate the values you want. However, and I think this is what you are going to say next, The entity object is bound directly because you are using it for data change and crud operations.

Ok, i'll have a ponder.
Walby 29-May-12 10:35am    
Ponder no more :).
I've been given the go-ahead to add a property/presentation object to the entity directly. As suggested in your earlier comment.

I'll mark your earlier comment as the answer if you like. You'll just have to re-post your comment as a solution.

Thanks db7uk.
db7uk 29-May-12 10:38am    
mmm... converter im afraid.

http://social.msdn.microsoft.com/Forums/en/wpf/thread/b0f18ca9-d236-42b4-9fe5-eec7de2cdd3d

1 solution

On your AddressDetails object, can you not have a readonly property that is exposed. This property could format the address1, address2 address3 values into a string. If address1, 2 or 3 is updated (and obviously throwing OnPropertyChangedEvent) you could also throw the OnPropertyChangedEvent on the readonly property thus firing the change notification you are after?
 
Share this answer
 
Comments
Walby 29-May-12 11:05am    
Worked perfectly.

I just ended up creating another property called AddressSummary, which called the ToString override I created earlier. Then made sure the other properties raised a notification for the AddressSummary property as their values changed.
db7uk 29-May-12 14:30pm    
Nice one.. Those little INotifyPropertyChanged!!!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900