2011-01-23 42 views
2

我有一個綁定到使用MVVM的屬性的combox控件。有一個在價值變動set方法進行驗證。該問題的價值得到改變,以新的價值,即使驗證失敗,而不是保留舊的價值..WPF MVVM Combox在驗證失敗時未保留舊值

下面是XAML:

<ComboBox Grid.Column="1" Grid.Row="1" Width="200" ItemsSource="{Binding Path=Applications, Mode=OneTime}" SelectedItem="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=DataContext.Application, Mode=TwoWay}" Margin="3"></ComboBox> 

下面是視圖模型代碼:

private string[] types = new string[] { "A", "B" }; 

private string application; 

public ObservableCollection<string> Applications { get; private set; } 

public Const() { 
     this.Applications = new ObservableCollection<string>(this.types.ToList()); 
    } 

public string Application { 
     get { 
      this.application = this.applicationSpecificRequirements.ContainsKey(Resources.ApplicationKey) ? this.applicationSpecificRequirements[Resources.ApplicationKey] : this.Applications[0]; 
      return this.application; 
     } 

     set { 
      if (this.exchangeViewModel.CheckIfApplicationNameExistsOrIsEmptyAndAssign(this.InstanceName, value)) { 
       System.Windows.Application.Current.Dispatcher.BeginInvoke(
        new Action(() => { 
         this.applicationSpecificRequirements[Resources.ApplicationKey] = this.application; 
         ((IHaveOnPropertyChangedMethod) this).OnPropertyChanged("Application"); 
        }), DispatcherPriority.ContextIdle, null); 

       return; 
      } 

      this.applicationSpecificRequirements[Resources.ApplicationKey] = value; 
     } 
    } 

回答

0

看起來像你在屬性setter最後一行缺少OnPropertyChanged(...)。

+0

嘗試但力度的工作,問題是,如果驗證失敗,它會進入if語句並設置值。但同樣不反映在用戶界面..同樣的作品,如果沒有驗證,然後價值越來越chnaged ..在這裏我需要保留舊值的情況下,驗證失敗 – Arihant 2011-01-25 08:27:41