Christian Kouamé
Christian Kouamé

Reputation: 348

How to update a field in a Silverlight 4 Datagrid?

I saw on multiple posts how to dynamically add and remove items from a DataGrid in Silverlight 4, but I'm searching for a way to Update just a field of an existing Line. The cell value is initialized with "OUI" Value, when I click on a button it must be changed to "NON". My code succefully update the Collection, but the DataGrid show the initial value until I manually click on the cell.

Here is my XAML

    <sdk:DataGrid x:Name="dtg" HorizontalAlignment="Left" Height="155" Margin="10,21,0,0" VerticalAlignment="Top" Width="380" AutoGenerateColumns="False" GridLinesVisibility="Horizontal" >
        <sdk:DataGrid.Columns>
            <sdk:DataGridTextColumn Binding="{Binding Lettrage, Mode=TwoWay}" CanUserSort="True" CanUserReorder="True" CellStyle="{x:Null}" CanUserResize="True" ClipboardContentBinding="{x:Null}" DisplayIndex="-1" DragIndicatorStyle="{x:Null}" EditingElementStyle="{x:Null}" ElementStyle="{x:Null}" Foreground="{x:Null}" FontWeight="Normal" FontStyle="Normal" HeaderStyle="{x:Null}" Header="Lettrage" IsReadOnly="False" MaxWidth="Infinity" MinWidth="0" SortMemberPath="{x:Null}" Visibility="Visible" Width="Auto"/>
        </sdk:DataGrid.Columns>
    </sdk:DataGrid>
    <Button Content="Button" HorizontalAlignment="Left" Margin="70,235,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>

And My code Behind :

public MainPage()
{               
     InitializeComponent();

     // Fill the datagrid
     source.Add(new Ligne());
     dtg.ItemsSource = source;
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{      
     string src = source.First().Lettrage;
     source.First().Lettrage = src == "OUI" ? "NON" : "OUI";           
}

Is it possible to do ? Thanks in advance.

Upvotes: 3

Views: 294

Answers (1)

Fede
Fede

Reputation: 44048

Your DataItem (The Ligne Class) must implement System.ComponentModel.INotifyPropertyChanged:

public class Ligne: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null) 
            handler(this, new PropertyChangedEventArgs(propertyName));
    }

    private string _lettrage;
    public string Lettrage
    {
        get { return _lettrage; }
        set 
        {
            _lettrage = value;
            OnPropertyChanged("Lettrage");
        }
    }
}

Upvotes: 2

Related Questions