我正在從事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類。 –