2017-08-18 75 views
-1

我必須在應用程序上綁定兩次數據。 我有兩個列表視圖。第一個是與複選框一起出現的所有元素的列表。 第二個列表視圖必須僅獲取複選框被選中的值。當ViewModel的值發生變化時,在ViewModel中應用濾鏡

所以我試圖處理一個ViewMode和兩個ObservableCollection。一個完整,一個將被過濾。但我沒有得到它的工作。這是做這件事的好方法嗎?

謝謝!

這裏是XAML代碼:

<!-- The full collection --> 
<ListView Grid.Row="1" Grid.Column="0" ItemsSource="{Binding Cycles.MyData}"> 
    <ListView.View> 
     <GridView AllowsColumnReorder="False"> 
      <GridViewColumn> 
       <GridViewColumn.CellTemplate> 
        <DataTemplate> 
         <CheckBox Style="{DynamicResource SwitchOnly}" IsChecked="{Binding Cycles.Used, UpdateSourceTrigger=PropertyChanged}"/> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn> 
      <GridViewColumn> 
       <GridViewColumn.CellTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding Name}"/> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
     </GridViewColumn> 
    </GridView> 
</ListView.View> 
<!-- The filtered collection --> 
<ListView Grid.Column="2" Grid.Row="0" Grid.RowSpan="2" ItemsSource="{Binding Cycles.MyDataFiltered}"> 
    <ListView.View> 
     <GridView AllowsColumnReorder="False"> 
      <GridViewColumn> 
       <GridViewColumn.CellTemplate> 
        <DataTemplate> 
         <CheckBox Style="{DynamicResource VisualOnly}" IsChecked="{Binding Check}"/> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn> 
      <GridViewColumn> 
       <GridViewColumn.CellTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding Name}"/> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn> 
     </GridView> 
    </ListView.View> 
</ListView> 

這裏是視圖模型:

public class ViewModel : PropertyChangedBase 
{ 
    public ObservableCollection<Cycles> MyData { get; set; } 
    public ObservableCollection<Cycles> MyDataFiltered { get; set; } 

    private bool _used = true; 
    public bool Used 
    { 
     get => _used; 
     set 
     { 
      _used = value; 
      _myDataView.Filter = Filter; 
      OnPropertyChanged("Used"); 
     } 
    } 

    private Cycles _currentSelectedCycle; 
    public Cycles CurrentSelectedFamily 
    { 
     get => _currentSelectedCycle; 
     private set 
     { 
      _currentSelectedCycle = value; 
      OnPropertyChanged("CurrentSelectedFamily"); 
     } 
    } 


    readonly ICollectionView _myDataView; 

    public ViewModel() 
    { 
     MyData = new ObservableCollection<Cycles>(); 
     MyDataFiltered = new ObservableCollection<Cycles>(); 
     _myDataView = CollectionViewSource.GetDefaultView(MyDataFiltered); 
     _myDataView.CurrentChanged += delegate 
     { 
      CurrentSelectedFamily = (Cycles)_myDataView.CurrentItem; 
     }; 
    } 
    private static bool Filter(object item) 
    { 
     var value = (Cycles)item; 
     return value != null && value.Check; 
    } 
} 

這裏是我的週期類:下面你的第二個列表綁定

public class Cycles : PropertyChangedBase 
{ 
    internal Cycles() 
    { 
     _check = false; 
     _firstDay = 1; 
    } 

    private bool _check; 
    private string _name; 
    private int _firstDay; 
    private int _lastDay; 

    public bool Check 
    { 
     get => _check; 
     set 
     { 
      if (_check == value) return; 
      _check = value; 
      OnPropertyChanged("Check"); 
     } 
    } 
    public string Name 
    { 
     get => _name; 
     set 
     { 
      if (_name == value) return; 
      _name = value; 
      OnPropertyChanged("Name"); 
     } 
    } 
    public int FirstDay 
    { 
     get => _firstDay; 
     set 
     { 
      if (_firstDay == value) return; 
      _firstDay = value; 
      OnPropertyChanged("FirstDay") 
     } 
    } 
    public int LastDay 
    { 
     get => _lastDay; 
     set 
     { 
      if (_lastDay == value) return; 
      _lastDay = value; 
      OnPropertyChanged("LastDay"); 
     } 
    } 
} 
public class PropertyChangedBase : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    protected void OnPropertyChanged(string propertyName) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 
+0

發表您的週期類。 – mm8

+0

drive.google.com/open?id=0B4bUNjAQRJpWMXpGSk1fRTVSeDA – Ramankingdom

回答

0

使用

ItemsSource="{Binding SelectedCycles}" 

和視圖模型

public IEnumerable<Cycles> SelectedCycles 
{ 
    get { return MyData.Where(x => x.Check== true); } 
} 
+0

我試過你的解決方案,當我調用它時,SelectedCycles被正確更新,但綁定它不更新。我必須補充什麼? – Thibaud

+0

您的Xaml窗口的DataContext應該是您的ViewModel。並且綁定不應該有循環。直接使用e.e SelectedCycles或MyData。 – Ramankingdom

+0

我在嘗試這個,而你的答案但仍然是相同的結果,我沒有在SelectedCycles中的元素:/它是我綁定選中的值的方式? – Thibaud

相關問題