我创建了自己的日历。我日历中的每一天都是一个itemsControl,它包含一个文本块和一个列表框,它应该按日期保存项目。
如何使用ItemsControl中绑定文本块中的字符串值过滤集合? 文本块与日期类的日期属性绑定。
视图模型
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>
答案 0 :(得分:1)
首先,ListBox的ItemsSource
绑定将不起作用,因为它的DataContext是Day对象,而SceneCollection
属性不存在,但在ViewModel中。
此外,您不应在ViewModel中过滤您的集合,因为所有项目都将绑定到它,并且它们需要不同的过滤器。
在你的情况下,如果你想在保持底层集合完整的同时使用过滤器和集合视图,我只需在你的'Day'类中添加一个'ICollectionView'属性,并为每一天分配一个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>