我正在从事WPF项目。我有这样一个类:WPF ItemsControl不刷新
public class Field
{
public ReservationDTO reservation { get; set; }
public DelegateCommand FieldChangeCommand { get; set; }
public bool Available { get; set; }
}
而且我有它的集合
public ObservableCollection<Field> Fields { get; set; }
我的看法是这样的
<ItemsControl ItemsSource="{Binding Fields}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid
Columns="{Binding Columns}"
Rows="{Binding Rows}"
/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Command="{Binding FieldChangeCommand}" CommandParameter="{Binding}" Content="{Binding reservation.Statusz}" FontSize="5" IsEnabled="{Binding Available}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这完全适用于第一次,当它负载。但是稍后我可能会修改一些Fields的值。例如:(视图模型代码的一部分)
Fields[SelectedIndex].reservation.Statusz = "Sold";
Fields[SelectedIndex].Available = false;
OnPropertyChanged("Fields");
OnPropertyChanged("Available");
是确保字段在此之后得到更新,因为在视图(如文本框)等元素刷新。但视图上的buttongrid不会使用Fields的新值更新。
有人可以告诉我为什么,以及如何解决这个问题?
你implem请在您的ReservationDTO类中使用INotifyPropertyChanged。如果没有,那么UI将不会收到您的Statusz属性已更改的通知。 – Bijington
@Bijington我有这样的ViewModelBase。 public abstract class ViewModelBase:INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; 保护ViewModelBase(){} 保护虚拟无效OnPropertyChanged([CallerMemberName]字符串propertyName的= NULL){ 如果 (的PropertyChanged!= NULL){ 的PropertyChanged(这一点,新PropertyChangedEventArgs(propertyName的)); } } } –
@Bijington如果我让我的Field类派生自ViewModelBase就够了吗?因为我不确定是否可以修改ReservationDTO类。 –