2015-10-12 143 views
-1

我的WCF應用程序支持從遠程服務下載書籍。 用戶向該服務發送請求以下載圖書,該服務獲取該請求並將書籍下載。作爲響應,它將基於枚舉值的下載進度發送給客戶端。基於枚舉的綁定屬性

我的觀點:
https://onedrive.live.com/redir?resid=3A8F69A0FB413FA4!123&authkey=!AJK3wiB_C_LqayQ&v=3&ithint=photo%2cpng

狀態是枚舉值。

public enum Status { Pending, Started, Completed }; 

BookModel

public class BookModel 
    { 
     public string Title { get; set; } 
     public string Author { get; set; } 
     public string Description { get; set; } 
     public Status Status { get; set; } 
    } 

我的任務是更新基於枚舉值的UI。

掛起 - 應顯示0%填充餅圖。

陳述 - 應顯示50%填充餅圖。

已完成 - 應顯示100%填充餅圖。

應更新的必需屬性是PieSlice對象(Book.xaml內部)的「EndAngle」(雙數據類型)。 我將此屬性綁定到BookViewModel中的「Percent」對象,但是當書籍狀態發生變化時,餅圖不會更新爲新的狀態值。

謝謝。

Book.xaml

<Label Grid.Row="0">Title</Label> 
     <Label Grid.Row="1">Author</Label> 
     <Label Grid.Row="2">Description</Label> 

     <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Title}"/> 
     <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Author}"/> 
     <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Description}"/> 
     <Button Grid.Column="2" Grid.RowSpan="3" Command="{Binding SaveCommand}" Content="Download" /> 


     <Ellipse Grid.Column="3" 
        Height="20" Width="20" 
        Stroke="Black" 
        StrokeThickness="0.5" 
        HorizontalAlignment="Center" 
        Grid.Row="1" 
        /> 
     <Controls:PieSlice Grid.Column="3" Grid.Row="1" Stroke="Black" Fill="Black" 
          Height="20" Width="20" 
          StartAngle="0" EndAngle="{Binding Percent}" 
          HorizontalAlignment="Center" /> 

BookViewModel

public class BookViewModel : ViewModelBase 
    { 
     private readonly BookModel Book; 

     /// <summary> 
     /// Initializes a new instance of the MainViewModel class. 
     /// </summary> 
     public BookViewModel(BookModel model) 
     { 
      this.Book = model; 
     } 


     public double Percent 
     { 
      get 
      { 
       return Book.Status == Status.Pending ? 0 : 
        Book.Status == Status.Started ? 180 : 
        Book.Status == Status.Completed ? 360 : 0; 
      } 
     } 
    public Status Status 
     { 
      get 
      { 
       return Book.Status; 
      } 
      set 
      { 
       Book.Status = value; 
       RaisePropertyChanged("Status"); 
      } 
     } 
} 

回答

0

通過居留制的制定者調用RaisePropertyChanged(「百分比」)的 視圖將查詢百分比,從而獲取正確的值。

視圖被界定爲Percent,一個計算出的屬性。當狀態更新時,百分比(屬性更新)但視圖綁定到百分比不會更新。這是因爲視圖不知道百分比已經改變(我們從來沒有告訴過它)。百分比也沒有支持屬性!

1

你沒有提出任何的PropertyChanged爲Percent財產。 你可以把RaisePropertyChanged("Percent");剛好在RaisePropertyChanged("Status");之後,所以當你改變Status的值時Percent通知也得到了提升。