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.