我定義了下面的 UI,它構建得很好。
當我運行它時,也沒有系結錯誤。
DEBUG 選項卡似乎包含 Collection 中定義的四個專案,但其他 TabView 為空。

我試圖查看我的過濾器內是否有任何斷點被擊中,但它們似乎沒有被擊中?
最小的例子:
主視窗.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>
主視窗.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"));
}
}
}
uj5u.com熱心網友回復:
您對CollectionViewSource.Source屬性的資料系結是錯誤的。它們當前系結到它們的DataContext,它不參考TestList資源(CollectionViewSource甚至沒有 aDataContext因為它沒有擴展FrameworkElement)。StaticResource您必須使用標記擴展來參考資源:
<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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/450983.html
上一篇:WMI列印機狀態始終為0
下一篇:目標c-OOP最佳實踐問題
