在 XAML 绑定中强制键入 IntelliSense 自动完成

Force type on IntelliSense autocompletion in XAML Binding

我正在处理一些相当大的 WPF 项目,其中包含大量 类 和 XAML 设计文件。

但有一件事让我抓狂:IntelliSense Binding 自动完成有时不会显示正确的值(主要是在我无法提供正确的 DataType 并且没有使用烘焙的情况下,例如 Page 内容类型)

因此实际问题是:是否有某种方法可以强制 IntelliSense 使用特定类型进行自动完成?

作为随机示例,以 XAML:

<DataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              DataType="{x:Type Accounting}">
    <ListView ItemsSource="{Binding Payments}">
        <ListView.View>
            <GridView>
                <!--
                    Auto completion still assumes the type is Accounting
                    and displays the properties of Accounting instead of
                    the required Payments.
                -->
                <GridViewColumn DisplayMemberBinding="{Binding Bank}"/>
            </GridView>
        </ListView.View>
    </ListView>
</DataTemplate>

这对于 C# 类:

public class Accounting
{
    public List<Payment> Payments { get; set; }
}

public class Payment
{
    public string Bank { get; set; }
}

您可以使用 {Binding Path=(xmlNameSpace:TypeName.PropertyName)} 形式强制类型并完成 PropertyName。

这会导致 Binding 将路径视为 attached property,当附加的 属性 类型是与绑定类型相同。我不确定是否有任何额外的开销试图将它解析为附加的 属性 或没有,但这足以让 Visual Studio 开始给你 auto-completion 作为属性你打字。我认为这有点 hack,因为它绝对不是这种语法的预期用途,

在您的具体示例中,它看起来像(根据您的命名空间进行调整):

<GridViewColumn DisplayMemberBinding="{Binding Path=(local:Payment.Bank)}"/>