更改数据网格中单元格中的值(绑定)

Change the value in a cell in a datagrid (binding)

这是我的问题。

当我 select 单行并单击按钮时,单元格必须更改值,但它不会更改任何内容

这是 XAML 代码。

        <DataGrid 

        ItemsSource="{Binding Dati_Viaggio, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
        SelectedItem="{Binding SelectDati_Viaggio, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

        AutoGenerateColumns="False" HorizontalAlignment="Left" Height="119" Margin="10,10,0,0" VerticalAlignment="Top" Width="497">

        <DataGrid.Columns>
            <DataGridTextColumn x:Name="NumOrd" Binding="{Binding Path=NumOrd, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  Header="NumOrd" Width="150" />
        </DataGrid.Columns>

    </DataGrid>

这是 C# 代码

Public ObservableCollection<Model_Ricerca_Dati_Viaggio> Dati_Viaggio 
{ get; set; }
private Model_Ricerca_Dati_Viaggio _SelectDati_Viaggio;
public Model_Ricerca_Dati_Viaggio SelectDati_Viaggio {
get { return _SelectDati_Viaggio; }
set {
    _SelectDati_Viaggio = value;
    OnPropertyChanged("SelectDati_Viaggio");}}

private string _NumOrd { get; set; }
public string NumOrd {
get { return _NumOrd; }
set {
    _NumOrd = value;
    OnPropertyChanged("NumOrd");}}

Private void Cmd_TrovaExe()
{
SelectDati_Viaggio.NumOrd = Now.@string;

OnPropertyChanged("NumOrd");
OnPropertyChanged("Dati_Viaggio");
OnPropertyChanged("SelectDati_Viaggio");
}

为什么单元格在 SelectDati_Viaggio.NumOrd = Now 后不刷新。@string; ?

您的 class Model_Ricerca_Dati_Viaggio 还必须实现 INotifyChanged 接口,以便将更改公开给 UI :

  public class Model_Ricerca_Dati_Viaggio:INotifyPropertyChanged
{
    private string _numOrd ;
    public string NumOrd  
    {
        get
        {
            return _numOrd;
        }

        set
        {
            if (_numOrd == value)
            {
                return;
            }

            _numOrd = value;
            OnPropertyChanged("NumOrd");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}