Home > OS >  DataGrid not filtering?
DataGrid not filtering?

Time:03-29

I have below UI defined and it builds fine.

When I run it there are no binding errors either.

The DEBUG tab seems to contain the four items defined in the Collection, however the other TabViews are empty.

enter image description here

I have tried to see if any breakpoints are hit inside my filters but they seem to not be hit?

Minimal example:

MainWindow.xaml

<Window x:Class="Dummy.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Dummy" >

    <Window.Resources>
        <local:TestList x:Key="TestList"/>
        <CollectionViewSource x:Key="NoScope" Source="{Binding TestList}" Filter="NoScopeFilter" />
        <CollectionViewSource x:Key="Scope" Source="{Binding TestList}" />
        <CollectionViewSource x:Key="ScopeCodeBehind" Source="{Binding TestList}" Filter="ScopeFilter" />
    </Window.Resources>

    <Grid>
        <TabControl>
            <TabItem Header="DEBUG">
                <DataGrid Name="DEBUG" AutoGenerateColumns="True" ItemsSource="{Binding Source={StaticResource TestList}}" />
            </TabItem>
            <TabItem Header="NoScope">
                <DataGrid Name="NoScope" AutoGenerateColumns="True" ItemsSource="{Binding Source={StaticResource NoScope}}" />
            </TabItem>
            <TabItem Header="Scope">
                <DataGrid Name="Scope" AutoGenerateColumns="True" ItemsSource="{Binding Source={StaticResource Scope}}" />
            </TabItem>
            <TabItem Header="Scope (CB)">
                <DataGrid Name="ScopeCB" AutoGenerateColumns="True" ItemsSource="{Binding Source={StaticResource ScopeCodeBehind}}" />
            </TabItem>
        </TabControl>
    </Grid>
</Window>

MainWindow.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;

namespace Dummy
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void NoScopeFilter(object sender, FilterEventArgs e)
        {
            if (e.Item is Test test)
            {
                e.Accepted = true;
            }
        }

        private void ScopeFilter(object sender, FilterEventArgs e)
        {
            if (e.Item is Test test)
            {
                e.Accepted = test.Scope.Equals("Scope", StringComparison.OrdinalIgnoreCase);
            }
        }

    }


    public abstract class Test
    {
        private string desciption;
        private string scope;

        public string Name { get; set; }
        public string Desciption { get => desciption; set => desciption = value; }
        public string ExpectedValue { get; set; }
        public string ActualValue { get; set; }
        public string Scope { get => scope; set => scope = value; }
        public abstract bool Result { get; }
        protected Test(string name, string expected, string actual, string scope = null)
        {
            this.Name = name;
            this.ActualValue = actual;
            this.ExpectedValue = expected;
            this.Scope = scope;
        }
    }

    public class IsEqual : Test
    {
        public IsEqual(string name, string expected, string actual, string scope = "") : base(name, expected, actual, scope)
        {
        }

        public override bool Result { get { return this.ActualValue == this.ExpectedValue; } }
    }

    public class IsNotEqual : IsEqual
    {
        public IsNotEqual(string name, string expected, string actual, string scope = "") : base(name, expected, actual, scope)
        {
        }

        public override bool Result { get { return !base.Result; } }
    }

    public class TestList : ObservableCollection<Test>
    {
        public TestList()
        {
            string actual = "test";
            string expected = "TesT";
            this.Add(new IsEqual("Should be equal", expected.ToLower(), actual));
            this.Add(new IsNotEqual("Should not be equal", expected, actual));
            this.Add(new IsEqual("Should be equal (with scope)", expected.ToLower(), actual, "scope"));
            this.Add(new IsNotEqual("Should not be equal (with another scope)", expected, actual, "another scope"));
        }

    }
}

CodePudding user response:

Your data bindings on the CollectionViewSource.Source properties are wrong. They are currently binding to their DataContext, which does not reference the TestList resource (CollectionViewSource doesn't even have a DataContext as it doesn't extend FrameworkElement). You must reference the resource using the StaticResource markup extension:

<Window.Resources>
  <local:TestList x:Key="TestList"/>
  <CollectionViewSource x:Key="NoScope" Source="{StaticResource TestList}" Filter="NoScopeFilter" />
  <CollectionViewSource x:Key="Scope" Source="{StaticResource TestList}" />
  <CollectionViewSource x:Key="ScopeCodeBehind" Source="{StaticResource TestList}" Filter="ScopeFilter" />
</Window.Resources>
  • Related