2017-04-16 27 views
0

我正在嘗試使用UWP社區工具包MasterDetailsView提供編輯界面。但是,我似乎無法獲取它的列表視圖中的單個項目,以便在進行更改時更新它們的屬性。在UWP社區工具包的MasterDetailsView中未正確更新的項目

下面是我使用的電纜鋪設控制的XAML的部分:

 <controls:MasterDetailsView x:Name="masterDetailsView" Foreground="Black" 
           ItemsSource="{x:Bind libraryVM.Recipes, Mode=TwoWay}" 
           SelectedItem="{x:Bind libraryVM.SelectedRecipe, Mode=TwoWay}" 
           NoSelectionContent="Select an item to view"> 
     <controls:MasterDetailsView.ItemTemplate> 
      <DataTemplate x:DataType="recipes:RecipeVM"> 
       <StackPanel Margin="0,8"> 
        <TextBlock Style="{ThemeResource SubtitleTextBlockStyle}" 
           Text="{x:Bind Name}" /> 
       </StackPanel> 
      </DataTemplate> 
     </controls:MasterDetailsView.ItemTemplate> 
... 

LibraryVM

public class LibraryVM : INotifyPropertyChanged 
{ 
    Library library; 
    ObservableCollection<RecipeVM> _Recipes; 
    public event PropertyChangedEventHandler PropertyChanged; 

    public LibraryVM() 
    { 
     library = new Library(); 
     _Recipes = new ObservableCollection<RecipeVM>(); 
     foreach (var rec in library.Recipes) 
     { 
      var recipe = _Recipes.FirstOrDefault(r => r.Id == rec.Id); 

      if (recipe == null) 
      { 
       var r = new RecipeVM(rec); 
       _Recipes.Add(r); 
      } 
      else 
      { 
       recipe.Name = rec.Name; 
       recipe.Description = rec.Description; 
      } 
     } 
    } 

    public ObservableCollection<RecipeVM> Recipes 
    { 
     get { return _Recipes; } 
     set 
     { 
      _Recipes = value; 
      OnPropertyChanged(); 
     } 
    } 

    RecipeVM _SelectedRecipe; 


    public RecipeVM SelectedRecipe 
    { 
     get 
     { 
      return _SelectedRecipe; 
     } 

     set 
     { 
      _SelectedRecipe = value; 
      OnPropertyChanged(); 
     } 
    } 

    public void SaveChanges() 
    { 
     if (_SelectedRecipe.Id == Guid.Empty) 
     { 
      library.CreateNew(_SelectedRecipe.Recipe); 
     } 
     else 
     { 
      library.SaveChanges(_SelectedRecipe.Recipe); 
     } 
    } 

    void OnPropertyChanged([CallerMemberName]string propertyName = "") 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

} 

RecipeVM

public class RecipeVM : INotifyPropertyChanged 
{ 
    Recipe _recipe; 
    Recipe _backup; 
    public event PropertyChangedEventHandler PropertyChanged; 

    public Recipe Recipe 
    { 
     get 
     { 
      return _recipe; 
     } 
    } 

    public RecipeVM(Recipe recipe = null) 
    { 
     _recipe = recipe; 

     if(_recipe == null) 
     { 
      _recipe = new Recipe(); 
      _recipe.Name = "New Recipe"; 
     } 

     this.IsEditing = false; 
    } 

    public Guid Id 
    { 
     get 
     { 
      return _recipe.Id; 
     } 

     set 
     { 
      _recipe.Id = value; 
      OnPropertyChanged(); 
     } 
    } 

    bool _IsEditing; 


    public bool IsEditing 
    { 
     get 
     { 
      return _IsEditing; 
     } 

     set 
     { 
      _IsEditing = value; 
      OnPropertyChanged(); 
     } 
    } 

    public string Name 
    { 
     get 
     { 
      return _recipe.Name; 
     } 

     set 
     { 
      _recipe.Name = value; 
      OnPropertyChanged(); 
     } 
    } 

    public string Description 
    { 
     get 
     { 
      return _recipe.Description; 
     } 

     set 
     { 
      _recipe.Description = value; 
      OnPropertyChanged(); 
     } 
    } 

    public void MakeBackup() 
    { 
     _backup = new Recipe() 
     { 
      Id = _recipe.Id, 
      Name = _recipe.Name, 
      Description = _recipe.Description 
     }; 
    } 

    public void RestoreBackup() 
    { 
     Name = _backup.Name; 
     Description = _backup.Description; 
    } 

    void OnPropertyChanged([CallerMemberName]string propertyName = "") 
    { 
     if(PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

然而,當我upda如果有任何RecipeVM項目,我似乎無法在MasterDetailsView的ListView中更新相應的項目。我相信我已經在任何適當的地方實施了INotifyPropertyChanged,但我顯然缺少一些東西。

更新 我已驗證綁定在其他地方起作用。如果在更改完成後逐步完成代碼,則RecipeVM和底層Recipe會被適當更新。

+0

更改您的** OnPropertyChanged()**的** OnPropertyChanged( 「」)**。示例** OnPropertyChanged(「Description」); ** – AVK

+0

@AVKNaidu在運行時[CallerMemberName]提供適當的名稱。我在調試過程中驗證了這一點。 – kettch

回答

0

榮譽給奇拉格沙阿,誰把我指向大致正確的方向。

{x:Bind Name}更改爲{Binding Name}工作,但不是因爲x:Bind有任何問題,而是我對此的理解。

我缺少的是x:Bind默認爲一個更保守的綁定模式。

關於它的mode財產,the documentation說:

指定綁定模式,因爲這些字符串之一: 「一次性」, 「單向」 或 「雙向」。默認值是「OneTime」。請注意,這與{Binding}在大多數情況下是「OneWay」的默認值不同。

更新我的XAML以明確聲明模式解決了問題。

<TextBlock Style="{ThemeResource SubtitleTextBlockStyle}" 
    Text="{x:Bind Name, Mode=OneWay}" /> 

另一個很好的參考,這個問題的答案:Difference between Binding and x:Bind

1

更改文本塊結合文本=「{綁定名稱}」