2016-10-12 123 views
0

什麼是最簡單的方法來綁定選定狀態的ListView的項目模型的布爾屬性?UWP列表視圖綁定

我有模式:

class Model { 
    public string Name { get; set; } 
    public bool Selected { get; set; } 
} 

和ListView:

<ListView x:Name="myListView" SelectionMode="Multiple"> 
<ListView.ItemTemplate> 
    <DataTemplate x:DataType="x:String"> 
     <TextBlock Text="{Binding Name}"/> 
    </DataTemplate> 
</ListView.ItemTemplate> 

我綁定物品列表視圖:

myListView.ItemsSource = // list of Model instances 

我想型號的Selected屬性總是重新在myListView中檢查它是否被選中。所以 - 通過選擇/取消選擇myListView中的項目,它將保存適當的bool值或通過設置Selected屬性myListView將選擇/取消選擇適當的項目。

+0

我想你應該可以使用[這個答案](HTTP:// stackoverflow.com/a/2511836/60761)。發佈回來讓我們知道如何/如何適用於UWP。 –

+0

謝謝。但是,我不確定從哪個代碼粘貼該答案。我將它粘貼在ListView標籤之間。然後我得到錯誤:類型不支持在Windows通用項目和成員IsSelected不reconnized或不可訪問。 –

回答

0

我認爲最完美的辦法是實行ListView.ItemSelectionChanged事件,並通過在列表視圖模型,將其設置爲true,這是選擇的項目,爲假,其餘項目的每個項目迭代。

然而,你可能想嘗試這樣的事情,但說實話我不知道這是否是正確的方法:

class Model { 
    public string Name { get; set; } 
    public bool Selected 
    { 
     get 
     { 
      return MyListView.SelectedItems.Count(x => x.Name == Name) > 0; 
     } 
    } 
} 
+0

如何在數據模型中找到「MyListView」? –

1

what is the easiest way to bind selected state of listview's item to model's boolean property?

我不知道這是最簡單方式,但對我來說,我認爲這是將SelectorItem.IsSelected propertyListViewItem與您的Selected屬性綁定的最簡單的方法。只有問題是,我們都知道ListView的每個項目都是ListViewItem的一個實例,但是當我們使用DataTemplate來構建ListViewItem的項目結構時,ListViewItem s在設計階段不可用。所以我的想法是在代碼此屬性綁定的背後,只是這裏舉例:

<ListView x:Name="myListView" SelectionMode="Multiple" Loaded="myListView_Loaded" ItemsSource="{x:Bind Collection}"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Name}" /> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

後面的代碼:

private ObservableCollection<Model> Collection = new ObservableCollection<Model>(); 

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    for (int i = 0; i < 100; i++) 
    { 
     Collection.Add(new Model { Name = "Name " + i }); 
    } 
} 

private void myListView_Loaded(object sender, RoutedEventArgs e) 
{ 
    IEnumerable<ListViewItem> lvItems = FindVisualChildren<ListViewItem>(myListView); 
    if (lvItems != null) 
    { 
     foreach (ListViewItem lvitem in lvItems) 
     { 
      Model model = lvitem.Content as Model; 
      Binding b = new Binding 
      { 
       Source = model, 
       Path = new PropertyPath("Selected"), 
       Mode = BindingMode.TwoWay, 
      }; 
      BindingOperations.SetBinding(lvitem, ListViewItem.IsSelectedProperty, b); 
     } 
    } 
} 

private static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject 
{ 
    if (depObj != null) 
    { 
     for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) 
     { 
      DependencyObject child = VisualTreeHelper.GetChild(depObj, i); 
      if (child != null && child is T) 
      { 
       yield return (T)child; 
      } 

      foreach (T childOfChild in FindVisualChildren<T>(child)) 
      { 
       yield return childOfChild; 
      } 
     } 
    } 
} 

public class Model : INotifyPropertyChanged 
{ 
    public string Name { get; set; } 

    private bool _Selected; 

    public bool Selected 
    { 
     get { return _Selected; } 
     set 
     { 
      if (value != _Selected) 
      { 
       _Selected = value; 
       OnPropertyChanged(); 
      } 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

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

這個效果很好,謝謝!然而,是否有辦法將執行Selected屬性綁定到IsSelectedProperty的C#代碼直接轉換爲XAML - 它會更具可讀性。 –

+0

代碼存在問題 - myListView具有可見性==當seriesListView_Loaded被調用時(我顯示myListView)導致綁定不被設置時摺疊。 –

+0

@MartinDusek,正如我在我的回答中所說的,在這種情況下,ListViewItem在xaml中不可用。所以我不能在xaml代碼中綁定它。您可以將此方法放在其他[ListView的事件]中(https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.listview.aspx?f=255&MSPPError=- 2147217396#events),但我認爲當你想讓它可見時執行這段代碼更好。我認爲這不是一個大問題。 –