WPF数据上下文可用于设计时间和运行时间 [英] WPF data context for design time and run time

查看:151
本文介绍了WPF数据上下文可用于设计时间和运行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习WPF,MVVM Light和ViewModelLocator模式,并在主窗口的数据上下文中遇到困难。

I'm learning WPF, MVVM Light and the ViewModelLocator pattern and running into difficulties with my main window's data context.

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; }
      }
   }

并在我的app.xaml中:

and in my 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}"

它可以编译,但是我绑定到xaml中其他地方的MainViewModel的所有属性显示为红色,提示为无法解析符号。我以为可以通过指定仅设计者的数据上下文来解决此问题:

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:

<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}">

,但是编译器不喜欢最后一行(位置,请参阅第一个逗号)。注意,我没有使用ExpressionBlend,但是我想我在一个课程中听说过这一行也将使VisualStudio设计器也可以使用:

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:

 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

如何在仍然允许Visual Studio在设计时识别绑定属性的同时使用ViewModelLocator?

How do I use a ViewModelLocator while still enabling Visual Studio to recognize bound properties at design time?

推荐答案

应使用默认数据上下文在设计时也可以使用:

Using the default data context should also work in design time:

DataContext="{Binding Main, Source={StaticResource Locator}}"

如果没有,请尝试编译该proyect并再次签出。
您可以使用 MvvmLight Toolkit IsInDesignMode 属性来管理要在设计时显示的属性值。 $ c>提供。默认情况下, MainViewModel 的构造函数如下:

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 IsInDesignMode property that the MvvmLight Toolkit provides. By default the MainViewModel's constructor looks like this:

    /// <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...

这篇关于WPF数据上下文可用于设计时间和运行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆