设计时和运行时的 WPF 数据上下文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24189815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF data context for design time and run time
提问by Jim C
I'm learning WPF, MVVM Light and the ViewModelLocator pattern and running into difficulties with my main window's data context.
我正在学习 WPF、MVVM Light 和 ViewModelLocator 模式,但在使用主窗口的数据上下文时遇到了困难。
public class ViewModelLocator
{
public ViewModelLocator()
{
var mainModel = new MainModel();
Main = new MainViewModel(mainModel);
}
public MainViewModel Main { get; private set; }
public static ViewModelLocator Instance
{
get { return Application.Current.Resources["Locator"] as ViewModelLocator; }
}
}
and in my app.xaml:
在我的 app.xaml 中:
<Application.Resources>
<viewModels:ViewModelLocator x:Key="Locator" />
</Application.Resources>
When I set the data context in my main window using:
当我使用以下方法在主窗口中设置数据上下文时:
DataContext="Binding Main, Source={StaticResource Locator}"
it compiles but all of MainViewModel's properies I bind to elsewhere in the xaml show up red with tooltip "cannot resolve symbol". I thought I could get around this by also specifying a designer-only data context:
它可以编译,但我绑定到 xaml 中其他地方的所有 MainViewModel 属性都显示为红色,并带有工具提示“无法解析符号”。我想我也可以通过指定一个仅供设计人员使用的数据上下文来解决这个问题:
<Window x:Class="WPFDemo.Windows.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:converters="clr-namespace:WPFDemo.Converters"
xmlns:local="clr-namespace:WPFDemo"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:models="clr-namespace:WPFDemo.Models"
xmlns:viewModels="clr-namespace:WPFDemo.ViewModels"
Title="MainWindow" Height="350" Width="525"
DataContext="Binding Main, Source={StaticResource Locator}"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance, Type=viewModels:MainViewModel,
IsDesignTimeCreatable=True}">
but the compiler doesn't like that last line ("The character ',' is unexpected at this position", referrring to the first comma). Note I'm not using ExpressionBlend, but I thought I heard in a course that this line would enable VisualStudio designer as well:
但是编译器不喜欢最后一行(“字符 ',' 在这个位置是意外的”,指的是第一个逗号)。注意我没有使用 ExpressionBlend,但我想我在课程中听说这条线也可以启用 VisualStudio 设计器:
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
How do I use a ViewModelLocator while still enabling Visual Studio to recognize bound properties at design time?
如何在使用 ViewModelLocator 的同时仍使 Visual Studio 能够在设计时识别绑定属性?
采纳答案by Raúl Ota?o
Using the default data context should also work in design time:
使用默认数据上下文也应该在设计时工作:
DataContext="{Binding Main, Source={StaticResource Locator}}"
If not, try to compile the proyect and check out again.
You can manage the properties values that you want to show in design time by using the IsInDesignModeproperty that the MvvmLight Toolkitprovides. By default the MainViewModel's constructor looks like this:
如果没有,请尝试编译项目并再次检查。您可以管理您要使用在设计时,显示属性值IsInDesignMode的属性MvvmLight Toolkit提供。默认情况下,MainViewModel的构造函数如下所示:
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
if (IsInDesignMode)
{
// Code runs in Blend --> create design time data.
}
else
{
// Code runs "for real"
}
}
Hope this helps...
希望这可以帮助...
回答by Contango
The last line will work nicely if you remove the comma after d:DesignInstance:
如果删除后面的逗号,最后一行会很好地工作d:DesignInstance:
d:DataContext="{d:DesignInstance Type=viewModels:MainViewModel,
IsDesignTimeCreatable=True}">

