我必须在我的应用程序上绑定两次数据。 我有两个列表视图。第一个是复选框中显示的所有元素的列表。 第二个列表视图必须仅获取选中复选框的值。
所以我试图处理一个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>
这是ViewModel:
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;
}
}
这是我的Cycle类:
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 :(得分:0)
在第二个列表中使用以下绑定
ItemsSource="{Binding SelectedCycles}"
并在视图模型中
public IEnumerable<Cycles> SelectedCycles
{
get { return MyData.Where(x => x.Check== true); }
}