2016-07-26 18 views
1

必要なDataAnnotationで飾られたCustomerプロパティを持つBoxというモデルがあります。UIコントロール無効です明らかにモデルが有効です

public class Box : ValidatableBindableBase 
{ 
     protected Customer _Customer; 
     [Required] 
     public virtual Customer Customer 
     { 
      get { return _Customer; } 
      set { SetProperty(ref _Customer, value); } 
     } 
} 

注:ValidatableBindableBaseは主にコンボボックスとボタンがあるUIでhttps://www.pluralsight.com/blog/software-development/async-validation-wpf-prism

から取られています。コンボボックスのItemsSourceは、プログラムの起動時にサービスコールによって満たされる顧客のリストです。

<ComboBox ItemsSource="{Binding Customers,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" 
        SelectedItem="{Binding Path=CurrentBox.Customer,UpdateSourceTrigger=PropertyChanged}" /> 

BoxのCustomersプロパティは、プログラムの開始時NULLです。したがって、コンボボックスは赤い枠線(Invalid)を持っています。ユーザーはOnBoxCommandを実行するボタンをクリックする必要があります。これはサービスコールを行い、CurrentBoxのCustomerプロパティを設定します。

public DelegateCommand<String> BoxCommand { get; private set; } 

protected async void OnBoxCommand() 
{ 
    CurrentBox.Customer = (await _ServiceCall.GetCustomer()); 
} 

OnBoxCommandは、コンボボックスを実行した後は、お客様がUIに設定されている、明らかにまだ赤い枠を得ました。 _ServiceCall.GetCustomer();行の後にデバッガを使用すると、CurrentBox.HasErrorsがfalseであり、CurrentBox.Customerが設定されていることがわかります。私はまた、バインディング "ValidatesOnException = True"に追加しようとし、CurrentBox.ValidateProperties()を呼び出しました。

私はいくつかの時間の後に、タスクにServiceCallの値を割り当てることによって解決策を見つけました。

protected async void OnBoxCommand() 
{ 
    var bla = (await _ServiceCall.GetCustomer()); 
    new Task(() => this.CurrentBox.Customer = bla).Start(); 
} 

wpfコントロールの検証がタスクなしで更新されなかった理由がわかりません。誰かにこの奇妙な行動のアイデアがあり、なぜ私が使用する必要があるのか​​説明できますnew Task(() => this.CurrentBox = bla).Start();

+0

「CurrentBox」とは何ですか?なぜあなたは顧客を設定しませんか? – lokusking

+0

私はうんざりです、私はコードスニペットを修正しました。 CurrentBoxはBox型です。 – Briefkasten

答えて

0

await _ServiceCall.GetCustomer();Task<Customer>を返します。サービスコールから顧客を取得するには、タスクikeの結果を呼び出す必要があります。

protected async void OnBoxCommand() 
{ 
    this.CurrentBox = await _ServiceCall.GetCustomer().Result; 

} 
関連する問題