我必須在應用程序上綁定兩次數據。 我有兩個列表視圖。第一個是與複選框一起出現的所有元素的列表。 第二個列表視圖必須僅獲取複選框被選中的值。當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));
}
}
發表您的週期類。 – mm8
drive.google.com/open?id=0B4bUNjAQRJpWMXpGSk1fRTVSeDA – Ramankingdom