C# 使用ItemsControl中TextBlock的值筛选列表框中的集合
我已经创建了自己的日历。我的日历中的每一天都有一个itemsControl,其中包含一个textblock和一个listbox,它应该包含每个日期的项目 如何使用ItemsControl中绑定的textblock中的字符串值筛选集合? textblock与Day类的date属性绑定 视图模型C# 使用ItemsControl中TextBlock的值筛选列表框中的集合,c#,wpf,xaml,binding,filter,C#,Wpf,Xaml,Binding,Filter,我已经创建了自己的日历。我的日历中的每一天都有一个itemsControl,其中包含一个textblock和一个listbox,它应该包含每个日期的项目 如何使用ItemsControl中绑定的textblock中的字符串值筛选集合? textblock与Day类的date属性绑定 视图模型 public ObservableCollection<Day> Days { get; set; } public ObservableCollection<Scene> S
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();
}
}
Xaml
首先,列表框的
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(示例),在天集合初始化中:
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;
}
}
private IEnumerable CreateDaysData()
{
var maxDays=DateTime.DaysInMonth(DateTime.Now.Year,DateTime.Now.Month);
对于(int d=1;d
{
e、 已接受=(即项目作为场景)。日期==天。日期;
});
day.Scenes=viewSource.View;
收益率回归日;
}
}
最后,您的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>
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;
}
}
<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>