3

我在多篇文章中看到了如何在 Silverlight 4 中从 DataGrid 中动态添加和删除项目,但我正在寻找一种仅更新现有 Line 的字段的方法。单元格值使用“OUI”值初始化,当我单击按钮时,它必须更改为“NON”。我的代码成功更新了集合,但 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";           
}

有可能吗?提前致谢。

4

1 回答 1

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");
        }
    }
}
于 2013-06-30T18:22:32.187 回答