【问题标题】:How to update a field in a Silverlight 4 Datagrid?如何更新 Silverlight 4 Datagrid 中的字段?
【发布时间】:2013-06-30 12:11:20
【问题描述】:

我在多篇文章中看到如何在 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";           
}

有可能吗? 提前致谢。

【问题讨论】:

    标签: c# silverlight


    【解决方案1】:

    您的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");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-09
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      相关资源
      最近更新 更多