首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使自定义控件上的DependencyProperty接受CollectionViewSource绑定?

问如何使自定义控件上的DependencyProperty接受CollectionViewSource绑定?
EN

Stack Overflow用户
提问于 2019-09-26 00:50:02
回答 1查看 210关注 0票数 0

背景:

(ItemsSource="{Binding Source={StaticResource ClustersView}}")

  • Filtering (ItemsSource="{Binding Source={StaticResource ClustersView}}")

  • Filtering I可以在XAML中创建一个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

代码:

代码语言:javascript
复制
<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
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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,你的问题迫使我重新阅读我认为我知道的东西!

相关代码在下面

代码语言:javascript
复制
    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);
            }
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58108191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档