对象引用未设置到对象实例,在 WPF 设计器中,位于 x:Reference

Object reference not set to an instance of an object, in WPF designer, at x:Reference

首先,我当然知道这是要问的最重复的问题之一...为了节省您的时间,我会说这有一个转折点。

一、设置

<UserControl {d:DesignInstance Type=MyControl, IsDesignTimeCreatable=True}>
    <Grid x:Name="root">
        <FrameElement x:Name="ContextProxy" Visibility="Collapsed" />
        ...
        <DataGridTextColumn.Visibility>
            <!-- squiggly line on the SOURCE part -->
            <Binding Source="{x:Reference Name=ContextProxy}"
                     Path=DataContext.IncludeThisColumn
                     />
        </DataGridTextColumn.Visibility>
    </Grid>
</UserControl>

代码隐藏

public partial class MyControl {

    // viewmodel for INPC (data lookups, child controls, etc)
    public ViewModels.vmMyControl { get; } = new ViewModels.vmMyControl();

    MyControl() { // ctor
        this.root.DataContext = this; // to support DependencyProperty
    }

    // several dependency properties for external use/bindings
    public static DP IncludeThisColumnDP = DP.Register(..., OnIncludeThisColumnChanged)
    public bool IncludeThisColumn { GetValue(DP); } { SetValue(DP, value); }

    // relay DP changes from parent / outside world, to internal ViewModel
    private void OnIncludeThisColumnChanged(..)
    { (o as vmMyControl).ViewModel.IncludeThisColumn = e.NewValue; }
}

视图模型

public class vmMyControl {

    private readonly myObservableINPC<bool> _IncludeThisColumn;
    public bool IncludeThisColumn { get/set for _IncludeThisColumn.Value }

    public vmMyControl() { // ctor
        // initialize INPC crap
        this.IncludeThisColumn = new blah(nameof(IncludeThisColumn));

        // data for designer
        if (...GetIsInDesignMode)
            Data_LoadMock();
    }

    public Data_LoadMock() {
        // populate some of the data points
    }
}

所以一般来说,我认为我正在遵循最佳实践(除了 DP/INPC 的混合,但它很好地满足了我的需求,所以无论如何)...


转折

所以,首先让我说,在 运行 时,代码有效。所有绑定、数据加载等。无一例外。这纯粹是一个设计时问题,但它让我很烦恼。

由于我知道在处理 WPF 设计模式时存在空引用问题,所以当我看到该问题时的第一个操作是添加:

MyControlTests.cs

[TestMethod]
public void InstantiateVM() {
    Action a = () => { return new vmMyControl(); }
    a.ShouldNotThrow(); // FluentAssertions
}

[TestMethod]
public void InstantiateUC() {
    Action a = () => { return new MyControl(); }
    a.ShouldNotThrow(); // FluentAssertions
}

事情是这样的……两个测试 运行 都成功了……经​​过检查(调试 + 断点),对象中没有任何属性导致异常。


环境

应用程序正在使用框架 4.5 和 C#6(因为我使用的是 VS2015)

我看到有人 post 关于 VS2015 将鼠标悬停在消息上以获取更多信息,但它似乎对我没有任何作用。

为了它的价值,我正在使用 VS2015 REL... 很想应用更新 2 或 3,但我 hearing/reading 似乎到处都有一些错误。

根据http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/

public class BindingProxy : Freezable
{
#region Overrides of Freezable

protected override Freezable CreateInstanceCore()
{
    return new BindingProxy();
}

#endregion

public object Data
{
    get { return (object)GetValue(DataProperty); }
    set { SetValue(DataProperty, value); }
}

// Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty DataProperty =
    DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
}

然后

<DataGrid.Resources>
    <local:BindingProxy x:Key="proxy" Data="{Binding}" />
</DataGrid.Resources>

<DataGridTemplateColumn.Visibility>
    <Binding Source="{StaticResource ResourceKey=DataContextProxy}"
             Path="Data.IncludeThisColumn"
             Converter="{StaticResource ResourceKey=BoolToHiddenConverter}"
             />
</DataGridTemplateColumn.Visibility>