English 中文(简体)
如何在收集意见中添加所附行为?
原标题:How can an attached behavior be added to a CollectionViewSource?

我正试图在收集意见书上添加所附行为,以便我能够在XAML的视像中提供过滤的预谋财产。

XAML喜欢:

<DataGrid ItemsSource="{Binding}">
    <DataGrid.DataContext>
        <CollectionViewSource Source="{Binding Path=Items}"
                 acb:CollectionViewSourceItemFilter.ItemFilter="{Binding Path=ItemFilter}" />
    </DataGrid.DataContext>
</DataGrid>

然而,我正遇到一个错误:

A  Binding  cannot be set on the  SetItemFilter  property of type   
 CollectionViewSource . A  Binding  can only be set on a DependencyProperty of a 
DependencyObject.

收款情况 观点似乎是一种依赖目标。 我不敢确定我做错做什么。

如下:

public static class CollectionViewSourceItemFilter
{
    /// <summary>
    /// Gets the property value.
    /// </summary>
    public static Predicate<object> GetItemFilter(CollectionViewSource collectionViewSource)
    {
        return (Predicate<object>)collectionViewSource.GetValue(ItemFilter);
    }

    /// <summary>
    /// Sets the property value.
    /// </summary>
    public static void SetItemFilter(CollectionViewSource collectionViewSource, Predicate<object> value)
    {
        collectionViewSource.SetValue(ItemFilter, value);
    }

    /// <summary>
    /// The ItemFilter dependency property.
    /// </summary>
    public static readonly DependencyProperty ItemFilter =
        DependencyProperty.RegisterAttached(
        "ItemFilter",
        typeof(Predicate<object>),
        typeof(ItemFilterBehavior),
        new UIPropertyMetadata(null, OnItemFilterChanged));

    private static void OnItemFilterChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
    {
        CollectionViewSource collectionViewSource = depObj as CollectionViewSource;
        if (collectionViewSource == null)
            return;

        if (!Equals(e.NewValue, e.OldValue))
        {
            var newFilter = (Predicate<object>)e.NewValue;

            // Remove any previous filter.
            ItemFilterBehavior oldBehavior;
            if (behaviors.TryGetValue(collectionViewSource, out oldBehavior))
            {
                oldBehavior.Unregister();
                behaviors.Remove(collectionViewSource);
            }

            if (newFilter != null)
                behaviors.Add(collectionViewSource, new ItemFilterBehavior(collectionViewSource, newFilter));
        }
    }

    private class ItemFilterBehavior
    {
        public ItemFilterBehavior(CollectionViewSource collectionViewSource, Predicate<object> filter)
        {
            _collectionViewSource = collectionViewSource;
            _filter = filter;
            _collectionViewSource.Filter += collectionViewSource_Filter;
        }

        void collectionViewSource_Filter(object sender, FilterEventArgs e)
        {
            e.Accepted = _filter(e.Item);
        }

        public void Unregister()
        {
            _collectionViewSource.Filter -= collectionViewSource_Filter;
        }

        private readonly CollectionViewSource _collectionViewSource;
        private readonly Predicate<object> _filter;
    }

    private static readonly IDictionary<CollectionViewSource, ItemFilterBehavior> behaviors = new ConcurrentDictionary<CollectionViewSource, ItemFilterBehavior>();
}
最佳回答

在该问题上张贴行为法典后,我发现了这一点。 我的扶养费使用项目FilterBehavior,而不是把“收听器”作为所有人。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签