Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6067335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:35:50+00:00 2026-05-23T09:35:50+00:00

Really the subject says it all. <CollectionViewSource x:Key=MyData Source={Binding} Filter={ SomethingMagicInXaml? } /> It’s

  • 0

Really the subject says it all.

<CollectionViewSource x:Key="MyData"
    Source="{Binding}" Filter="{ SomethingMagicInXaml? }" />

It’s not that I can’t have code behind. It just nags at me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:35:50+00:00Added an answer on May 23, 2026 at 9:35 am

    You can do pretty much anything in XAML if you “try hard enough”, up to writing whole programs in it.

    You will never get around code behind (well, if you use libraries you don’t have to write any but the application still relies on it of course), here’s an example of what you can do in this specific case:

    <CollectionViewSource x:Key="Filtered" Source="{Binding DpData}"
                          xmlns:me="clr-namespace:Test.MarkupExtensions">
        <CollectionViewSource.Filter>
            <me:Filter>
                <me:PropertyFilter PropertyName="Name" Value="Skeet" />
            </me:Filter>
        </CollectionViewSource.Filter>
    </CollectionViewSource>
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Markup;
    using System.Windows.Data;
    using System.Collections.ObjectModel;
    using System.Windows;
    using System.Text.RegularExpressions;
    
    namespace Test.MarkupExtensions
    {
        [ContentProperty("Filters")]
        class FilterExtension : MarkupExtension
        {
            private readonly Collection<IFilter> _filters = new Collection<IFilter>();
            public ICollection<IFilter> Filters { get { return _filters; } }
    
            public override object ProvideValue(IServiceProvider serviceProvider)
            {
                return new FilterEventHandler((s, e) =>
                    {
                        foreach (var filter in Filters)
                        {
                            var res = filter.Filter(e.Item);
                            if (!res)
                            {
                                e.Accepted = false;
                                return;
                            }
                        }
                        e.Accepted = true;
                    });
            }
        }
    
        public interface IFilter
        {
            bool Filter(object item);
        }
    
        // Sketchy Example Filter
        public class PropertyFilter : DependencyObject, IFilter
        {
            public static readonly DependencyProperty PropertyNameProperty =
                DependencyProperty.Register("PropertyName", typeof(string), typeof(PropertyFilter), new UIPropertyMetadata(null));
            public string PropertyName
            {
                get { return (string)GetValue(PropertyNameProperty); }
                set { SetValue(PropertyNameProperty, value); }
            }
            public static readonly DependencyProperty ValueProperty =
                DependencyProperty.Register("Value", typeof(object), typeof(PropertyFilter), new UIPropertyMetadata(null));
            public object Value
            {
                get { return (object)GetValue(ValueProperty); }
                set { SetValue(ValueProperty, value); }
            }
            public static readonly DependencyProperty RegexPatternProperty =
                DependencyProperty.Register("RegexPattern", typeof(string), typeof(PropertyFilter), new UIPropertyMetadata(null));
            public string RegexPattern
            {
                get { return (string)GetValue(RegexPatternProperty); }
                set { SetValue(RegexPatternProperty, value); }
            }
    
            public bool Filter(object item)
            {
                var type = item.GetType();
                var itemValue = type.GetProperty(PropertyName).GetValue(item, null);
                if (RegexPattern == null)
                {
                    return (object.Equals(itemValue, Value));
                }
                else
                {
                    if (itemValue is string == false)
                    {
                        throw new Exception("Cannot match non-string with regex.");
                    }
                    else
                    {
                        return Regex.Match((string)itemValue, RegexPattern).Success;
                    }
                }
            }
        }
    }
    

    Markup extensions are your friend if you want to do something in XAML.

    (You might want to spell out the name of the extension, i.e. me:FilterExtension as the on-the-fly checking in Visual Studio may complain without reason, it still compiles and runs of course but the warnings might be annoying.
    Also do not expect the CollectionViewSource.Filter to show up in the IntelliSense, it does not expect you to set that handler via XML-element-notation)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Subject says it all really...Is there anywhere a good tutorial for Xcode's Debugger out
The subject really says it all: Is there a WPF progressbar like the Windows
As the subject says, really! What do they do?
There's not a lot to add to the subject really. I am after a
I really have read the other articles that cover this subject. But I seem
They brush the subject in this thread, but it does not really answer much:
The subject says it all - normally easy and cross platform way is to
The subject really says my goal, but I'm stumped as to the implementation. I
I tried to google common info about subject, but i really can't find some
sorry for lamer question, but I really could not found subject. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.