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

标签 c# silverlight

我在多篇文章中看到了如何在 Silverlight 4 的 DataGrid 中动态添加和删除项目,但我正在寻找一种方法来仅更新现有行的一个字段。单元格值初始化为“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";           
}

有可能吗? 提前致谢。

最佳答案

您的DataItem(Ligne 类)必须实现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");
        }
    }
}

关于c# - 如何更新 Silverlight 4 Datagrid 中的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17390231/

相关文章:

c# - 使用 LINQ 查找最常用的单词

c# - JwtSecurityToken 与 SecurityTokenDescriptor 中的日期不同

wcf - 可以做些什么来加快同步 WCF 调用?

wcf - 调试 IIS Express Web 服务/silverlight 应用程序时出现安全错误

c# - Modelbinder 创建集合项的新实例而不是更新现有实例

c# - C# "using"语句什么时候最有用?

sql-server - Silverlight 应用程序中的压力测试,它使用来自 WCF DataService 的数据,然后公开来自 SQLServer 数据库的数据

c# - 如何在 Windows Phone Silverlight 中获取 UIElement 的角度?

silverlight - 隔离存储站点设置和应用程序设置之间有什么区别?

c# - 与c#计算机程序和java android应用程序的数据库连接