如果源更改,DataGridTemplateColumn中的TextBlock不会刷新

时间:2015-04-10 02:52:55

标签: wpf xaml

DataGridTemplateColumn 中有一个 TextBlock ,它最初绑定到ViewModel属性(Guest.FirstNames),正确显示值。但是,当ViewModel中的该属性更改时,更改不会反映在视图中。以下是相关代码

RoomSlot

public class RoomSlot : EntityBase {
    [Required]
    public Guest Guest { get; set; }
}

访客

public class Guest : EntityBase {

    private string _firstNames;
    [Required(ErrorMessage = "FirstNames is Required")]
    public string FirstNames {
        get { return _firstNames; }
        set { _firstNames = value; NotifyOfPropertyChange(() => FirstNames); }
    }
    public List<RoomSlot> RoomSlots { get; set; }
}

查看

<!-- DataGrid.DataContext = ObservableCollection<RoomSlot> -->
<!-- DataGridTemplateColumn.DataContext = RoomSlot -->

<DataGridTemplateColumn Header="Guest" Width="auto" >
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" >
                <TextBlock Text="{Binding Guest.FirstNames, Mode=OneWay}"
                           FontWeight="Normal"/>
            </StackPanel>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

视图模型

public class MakeReservationViewModel : Conductor<object> {

    private ObservableCollection<RoomSlot> _roomSlots;
    public ObservableCollection<RoomSlot> RoomSlots {
        get { return _roomSlots; }
        set { _roomSlots = value; NotifyOfPropertyChange(() => RoomSlots); }
    }

    //changedRecord contains updated Guest
    public void Handle(RecordChanged<Guest> changedRecord) {
        int i = 0;
        foreach (RoomSlot slot in RoomSlots) {
            if (slot == currentlyEditingSlot) {
                slot.Guest = changedRecord.Record;
                NotifyOfPropertyChange(() => RoomSlots[i].Guest.FirstNames);
                NotifyOfPropertyChange(() => RoomSlots);
            }
            i++;
        }
   }
}

如您所见,我正在更改 Guest.FirstNames 访客 RoomSlot 的属性。在ViewModel中更新时, Guest.FirstNames 不会反映View中的更改。为什么TextBlock不会刷新,尽管提升了NotifyOfPropertyChanged(校准微实现的INotifyPropertyChagned)

修改

这是在ViewModel

//This is where I Initially receive new RoomSlots from another class
public void Handle(List<RoomSlot> roomSlots) {
    RoomSlots = new ObservableCollection<RoomSlot>(roomSlots);
    //If I Apply Guest Names here as given below, they appear correctly in View
    foreach (RoomSlot slot in RoomSlots) {
        slot.Guest.FirstNames = "Sarah Anderson";
    }
}

以上代码首次初始化ViewModel ObservableCollection属性。此时,访客姓名不详,但如果我(用于测试)初始化Guest.FirstNames,我正确地使绑定工作,并且“Sarah Anderson”出现在DataGridTemplateColumn中。

编辑2
最后,如果我更改 RoomSlot.Guest

的定义,它会起作用

来自

public class RoomSlot : EntityBase {
    [Required]
    public Guest Guest { get; set; }
}

至此

public class RoomSlot : EntityBase {

    private Guest _guest; 
    [Required]
    public Guest Guest {
        get { return _guest; }
        set { _guest = value; NotifyOfPropertyChange(() => Guest); }
    }

}

虽然我不确定为什么明确提出NotifyOfPropertyChange(() => RoomSlots[i].Guest)不起作用,但上述改变

0 个答案:

没有答案