我在多篇文章中看到了如何在 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/