Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XAML WPF DataGridView MVVM , +
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.
 
<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:
 
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.

<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 29-May-12 3:04am
Edited 29-May-12 3:16am
v4
Comments
db7uk at 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?
SeniorCrispy at 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 at 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.
SeniorCrispy at 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 at 29-May-12 10:38am
   
mmm... converter im afraid.
 
http://social.msdn.microsoft.com/Forums/en/wpf/thread/b0f18ca9-d236-42b4-9fe5-eec7de2cdd3d
db7uk at 29-May-12 10:38am
   
ok cool. Thats good! im just adding it as a solution. If you could mark as answer that would be wicked!

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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?
  Permalink  
Comments
SeniorCrispy at 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 at 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)

  Print Answers RSS
0 BillWoodruff 309
1 Sergey Alexandrovich Kryukov 290
2 George Jonsson 274
3 CPallini 255
4 Gihan Liyanage 205
0 OriginalGriff 4,895
1 CPallini 4,010
2 Sergey Alexandrovich Kryukov 3,514
3 George Jonsson 2,826
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 29 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100