背景:
(ItemsSource="{Binding Source={StaticResource ClustersView}}")
ItemsSource="{Binding Source={StaticResource ClustersView}}")
CollectionViewSource --从ObservableCollection获取数据--从CollectionViewSource到DataGrid --行为与预期的CollectionViewSource操作完全一致。问题:
我无法(或者不知道如何)在我的DependencyProperty上创建一个CustomControl,它将绑定到CollectionViewSource,并在ColelctionViewSource视图发生变化时更新/触发(例如,过滤更改时)。
DependencyProperty CollectionViewSource**?** 应该是什么类型,以便我可以绑定到CollectionViewSource**?** Object类型,显示绑定CollectionViewSource将作为ListCollectionView传递给我的CustomControl DependencyProperty -但是ListCollectionView没有提供一个事件(即可见的事件)。而不是Internal( CollectionChanged )。
将ItemsControl派生为我的CustomControl的BaseClass不是一个选项。附带的评论--我不需要从ItemsControl派生来实现这个功能:我如何创建一个包含两个绑定的CustomControl,这些绑定可以接受两个不同的CollectionViewSources (__ItemsControl不能这样做)?
代码:
CollectionViewSource "ClustersView“
DataGrid:ItemsSource被绑定到"ClustersView“(这很好)
NavigatorControl:这是我的CustomControl
代码:
<CollectionViewSource x:Key="ClustersView"
Source="{Binding EventClusters, ElementName=me}"
Filter="ClustersViewSource_Filter"></CollectionViewSource>
<DataGrid ItemsSource="{Binding Source={StaticResource ClustersView}}">
<LightMapperControls:NavigatorControl Clusters="{Binding Source={StaticResource ClustersView}}">
</LightMapperControls:NavigatorControl>
//The DP on NavigatorControl that I bind the CollectionViewSource to
public object Clusters
{
get { return (object)GetValue(ClustersProperty); }
set { SetValue(ClustersProperty, value); }
}
// Using a DependencyProperty as the backing store for Clusters. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ClustersProperty =
DependencyProperty.Register("Clusters", typeof(object), typeof(NavigatorControl), new PropertyMetadata(null, OnClustersChanged));
private static void OnClustersChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
ListCollectionView lcv = (ListCollectionView)e.NewValue; //e.NewValue is of type ListCollectionView
//lcv.Items //no such property
//lcv.CollectionChanged //no such event
}发布于 2019-09-26 20:56:34
解决方案是让WPF 完成所有的工作!I创建了object类型的Clusters DependencyObject,然后添加FrameworkPropertyMetadataOption.AffectsRender来触发OnRender,以便每当Clusters更改时重新绘制画布(即。过滤发生在CollectionViewSource上)。(重新绘制画布的实际方法是从DrawClusters调用OnRender)。DrawClusters包含Clusters (object)到ICollectionView的强制转换,我在for..each循环中枚举它来访问每个单独的Cluster。
谢谢你@PeterDuniho,你的问题迫使我重新阅读我认为我知道的东西!
相关代码在下面
public object Clusters
{
get { return (object)GetValue(ClustersProperty); }
set { SetValue(ClustersProperty, value); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ClustersProperty =
DependencyProperty.Register("Clusters", typeof(object), typeof(NavigatorControl), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
private void DrawClusters(ref DrawingContext dc, ref double SquareWidth, ref double SquareHeight)
{
if (Clusters == null)
return;
if (ClustersVisibility == Visibility.Visible)
{
ICollectionView cv = (ICollectionView)Clusters;
foreach (ClusterBase cluster in cv)
{
PathGeometry geometry = new PathGeometry();
foreach (ClusterSquare square in cluster.Squares)
{
geometry = Geometry.Combine(geometry, new RectangleGeometry(square.ToRect(SquareWidth, SquareHeight)), GeometryCombineMode.Union, null);
}
dc.DrawGeometry(clustersbrush, clusterspen, geometry);
}
}
}https://stackoverflow.com/questions/58108191
复制相似问题