使用ItemsControl中的TextBlock值在ListBox中过滤集合
本文关键字:过滤 集合 ListBox 值在 ItemsControl 中的 TextBlock 使用 | 更新日期: 2023-09-27 18:03:26
我创建了自己的日历。在我的日历中,每一天都是一个itemsControl,它包含一个文本块和一个列表框,其中应该包含每个日期的项目。
我如何过滤一个集合使用字符串值从绑定的文本块在ItemsControl?textblock与Day类的date属性绑定。
视图模型
public ObservableCollection<Day> Days { get; set; }
public ObservableCollection<Scene> SceneList;
private ListCollectionView _sceneCollection;
public ListCollectionView SceneCollection
{
get
{
if (_sceneCollection == null) //important for loading the app
{
_sceneCollection = new ListCollectionView(this.SceneList);
_sceneCollection.IsLiveFiltering = true;
_sceneCollection.Filter = o =>
{
var Scene = o as Scene;
return Scene != null && Scene.Date == ////string of binded TextBlock//;
};
}
return _sceneCollection;
}
set
{
_sceneCollection = value; RaisePropertyChanged();
}
}
模型public class Day : INotifyPropertyChanged
{
private DateTime date;
public DateTime Date
{
get { return date; }
set
{
date = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Date"));
}
}
}
Xaml
<ItemsControl ItemsSource="{Binding Days}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="6" Columns="7">
</UniformGrid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Date , Converter={StaticResource DateConverter}, ConverterParameter=DAY}"/>
<ListBox ItemsSource="{Binding SceneCollection}" dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True" Height="100">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock>
<Run Text="{Binding Path=SceneNumber}"/>
<Run Text="{Binding Path=SlugLine}"/>
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
首先,您的ListBox的ItemsSource
绑定将不起作用,因为它的DataContext是Day对象,并且SceneCollection
属性不存在,但在您的ViewModel中。
同样,你不应该在ViewModel中过滤你的集合,因为所有的项目都将绑定到它,它们将需要不同的过滤器。
在你的情况下,如果你想使用过滤器和集合视图,同时保持底层集合完整,我只需添加一个'ICollectionView'属性到你的'Day'类,并分配每个Day你的SceneCollection的过滤视图。
模型:
public class Day : INotifyPropertyChanged
{
private DateTime date;
public DateTime Date
{
get { return date; }
set
{
date = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Date"));
}
}
private ICollectionView scenes;
public ICollectionView Scenes
{
get { return scenes; }
set
{
scenes = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Scenes"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
ViewModel(示例),在您的Days集合初始化中:
private IEnumerable<Day> CreateDaysData()
{
var maxDays = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
for (int d = 1; d <= maxDays; d++)
{
var day = new Day
{
Date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, d)
};
var viewSource = new CollectionViewSource
{
Source = ScenesCollection
};
viewSource.Filter += new FilterEventHandler((o, e) =>
{
e.Accepted = (e.Item as Scene).Date == day.Date;
});
day.Scenes = viewSource.View;
yield return day;
}
}
最后,您的XAML将像这样结束:
<ItemsControl ItemsSource="{Binding Days}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="6" Columns="7">
</UniformGrid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Date , Converter={StaticResource DateConverter}, ConverterParameter=DAY}"/>
<!-- The ListBox's ItemsSource is bound to the ICollectionView of your Day class -->
<ListBox ItemsSource="{Binding Scenes}" dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True" Height="100">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock>
<Run Text="{Binding Path=SceneNumber}"/>
<Run Text="{Binding Path=SlugLine}"/>
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>