Kman
Kman

Reputation: 5001

Silverlight MVVM binding and use of extended class property

I've got a class with a city and country property, which I extend to combine the two values.

public class Area
{
public string City;
public string Country;
}

This is exposed to Silverligt by a RIA Service and I have a extension

public partial class Area
{
public string AreaString
{
get { return City + ", " + Country;}
}

In my XAML a datagrid is using the AreaString

<sdk:DataGrid AutoGenerateColumns="False" 
                      ItemsSource="{Binding Path=AreaCollection}"
                      Name="dataGrid1" Width="Auto">
    <sdk:DataGrid.Columns>
        <sdk:DataGridTextColumn CanUserReorder="True" IsReadOnly="True"  Width="Auto" Binding="{Binding Path=AreaString}" />                
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>

This datagrid is bound to a dataform where I may edit the Country and City properties with a two way binding. But how am I supposed to get the datagrid "updated" with the new values? To trigger a new get of the AreaString property.

Upvotes: 0

Views: 416

Answers (2)

O.O
O.O

Reputation: 11287

Add OnPropertyChanged like below after you implement INotfiyPropertyChanged interface:

public partial class Area
{
    public string AreaString
    {
        get 
        { 
            return City + ", " + Country;
            OnPropertyChanged("AreaString");
        }
    }
}

You should also add OnPropertyChanged("AreaString"); in the City AND Country setters.

Update

An alternative to this is to extend either the OnCityChanged or the OnCityChanging partial methods that exist with your entity. Like:

partial void OnCityChanged()
{
    OnPropertyChanged(new PropertyChangedEventArgs("AreaString"));
}

Upvotes: 1

Felice Pollano
Felice Pollano

Reputation: 33252

When you touch City or Country you should notify the PropertyChanged event for AreaString too. In order to achieve this, you should also make Area implement INotifyPropertyChanged A sample on how to use it is here.

Upvotes: 2

Related Questions