如何更新Silverlight 4 Datagrid中的字段?

时间:2013-06-30 12:11:21

标签: c# silverlight

我在多篇文章中看到如何在Silverlight 4中动态添加和删除DataGrid中的项目,但我正在寻找一种方法来更新现有Line的字段。单元格值使用“OUI”值初始化,当我单击按钮时,必须将其更改为“NON”。我的代码成功更新了Collection,但DataGrid显示了初始值,直到我手动点击单元格。

这是我的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"/>

我的代码背后:

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";           
}

有可能吗? 提前谢谢。

1 个答案:

答案 0 :(得分:2)

您的DataItemLigne班级)必须实施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");
        }
    }
}