2013-06-30 10 views
3

我對多個職位瞭如何動態地添加和在Silverlight 4 DataGrid中刪除項目,但我在尋找一種方式來更新只是現有線路的現場。單元格值用「OUI」值初始化,當我點擊一個按鈕時,它必須改爲「NON」。我的代碼成功地更新了Collection,但DataGrid顯示初始值直到我手動單擊單元格。如何更新Silverlight 4 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";   
} 

是否有可能呢? 在此先感謝。

回答

2

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"); 
     } 
    } 
} 
+0

偉大的作品!謝謝 –

相關問題