由于未知的DataContext,无法解析符号"MyVariable"
请问该如何解决?
由于未知的DataContext,无法解析符号"MyVariable"
请问该如何解决?
DataContext
未正确设置。binding
进行Ctrl-Click来自动导航到C#类;在Visual Studio的“属性”窗格中,您可以为所选控件选择数据上下文:
Blend 也可以用于设置数据上下文。在 Blend 中打开您的 .sln 文件,选择设计元素,然后在属性中选择“新建”:
DevExpress也可以通过其向导帮助您解决XAML中的错误。
在XAML中,选择要为其设置数据上下文的父元素(通常是整个表单),然后在设计器中选择操作三角形。
然后,浏览到具有C#代码的类。
提示:除非您向类添加一个无参数的构造函数,否则该类将不可见。
<UserControl x:Class="DemoAllocation.MyActualView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Implementation="clr-namespace:DemoAllocation.ViewModel.Implementation" x:Class="DemoAllocation.MyActualView"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.DataContext>
<Implementation:MyActualViewModel/>
</UserControl.DataContext>
如果您在WPF设计器上看不到智能标记,请检查它们是否在某个时候被关闭:
可以在启动时添加一段代码片段,每当发生绑定错误时弹出消息框。这被证明非常有用。
如果上述网站链接失效了,这里是代码:
public partial class Window1 : Window
{
public Window1()
{
BindingErrorTraceListener.SetTrace();
InitializeComponent();
}
}
方法:
using System.Diagnostics;
using System.Text;
using System.Windows;
namespace SOTC_BindingErrorTracer
{
public class BindingErrorTraceListener : DefaultTraceListener
{
private static BindingErrorTraceListener _Listener;
public static void SetTrace()
{ SetTrace(SourceLevels.Error, TraceOptions.None); }
public static void SetTrace(SourceLevels level, TraceOptions options)
{
if (_Listener == null)
{
_Listener = new BindingErrorTraceListener();
PresentationTraceSources.DataBindingSource.Listeners.Add(_Listener);
}
_Listener.TraceOutputOptions = options;
PresentationTraceSources.DataBindingSource.Switch.Level = level;
}
public static void CloseTrace()
{
if (_Listener == null)
{ return; }
_Listener.Flush();
_Listener.Close();
PresentationTraceSources.DataBindingSource.Listeners.Remove(_Listener);
_Listener = null;
}
private StringBuilder _Message = new StringBuilder();
private BindingErrorTraceListener()
{ }
public override void Write(string message)
{ _Message.Append(message); }
public override void WriteLine(string message)
{
_Message.Append(message);
var final = _Message.ToString();
_Message.Length = 0;
MessageBox.Show(final, "Binding Error", MessageBoxButton.OK,
MessageBoxImage.Error);
}
}
}
使用免费工具Snoop。
有一个非常好的功能可以按控件绑定错误进行过滤。这使您可以直接导航到具有绑定错误的可视化界面。
启动Snoop后:
实际上有两个完全不同的DataContext:设计时和运行时。
前面的大部分解决方案都是关注设置运行时DataContext。
一旦设置了设计时DataContext,Visual Studio或Blend中的XAML预览将显示自定义C#类提供的自定义数据。
如果使用Blend,则可以从XML文件中读取此自定义数据,但我更喜欢从自己的C#类中提供它。
要设置设计时DataContext,请参见:
或者,将此代码添加到任何元素中(这将在设计时新建 MyClass
类,因此Intellisense将正常工作):
d:DataContext="{d:DesignInstance d:Type=viewModel:MyClass, IsDesignTimeCreatable=True}"
并将此添加到标题:
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
请注意,仅当您使用用户控件时,才会出现XAML预览。如果您更喜欢使用DataTemplates,没问题:您可以创建一个临时用户控件,其中包括DataTemplate,并将“设计时”DataContext设置为指向静态类。编写静态类的代码,以便它创建ViewModel的新实例(即您想要绑定到的类)。例如,您的静态类可以从数据库读取数据,填充ViewModel的属性,您可以在XAML设计时使用来自数据库的实时数据。
这种技术也可以与依赖注入(如Unity或MEF)完美配合使用。您需要将设计时DataContext指向一个静态类,该类从依赖注入容器中获取适当的类,并设置所有内容。然后,您可以在XAML预览中实时查看数据。前面提到的链接演示了这种工作方式(包括在XAML设计时间内实时跳动的时钟的YouTube视频!)。