【问题标题】:WPF & MVVM, design time data in Window, but not child UserControlWPF & MVVM,Window 中的设计时数据,但不是子 UserControl
【发布时间】:2014-05-26 15:14:10
【问题描述】:

在 Visual Studio 2013(完全更新)和 Blend 2013 中,我没有在我的 UserControl 中看到设计时数据,但我在具有 UserControl 的窗口中看到了设计时数据。以下是我的问题的简化演示。

模型(color.cs):

using System;
namespace TestWPF {
    public class color {
        public string name { get; set; }
    }
}

模型视图(colorViewModel.cs):

using System;
using System.Collections.Generic;

namespace TestWPF
{
    public class colorViewModel
    {
        public List<color> colorList;
        public colorViewModel()
        {
            colorList = new List<color>();
            colorList.Add(new color() { name = "blue" });
            colorList.Add(new color() { name = "red" });
        }
    }
}

UserControl 代码隐藏 (colorUserControl.xaml.cs):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TestWPF
{
    public partial class colorUserControl : UserControl
    {
        public colorUserControl()
        {
            InitializeComponent();
            this.DataContext = (new colorViewModel()).colorList;
        }
    }
}

UserControl XAML (colorUserControl.xaml):

<UserControl x:Class="TestWPF.colorUserControl"
             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">
    <Grid>
        <DataGrid ItemsSource="{Binding}" AutoGenerateColumns="True" />
    </Grid>
</UserControl>

窗口 XAML (MainWindow.xaml):

<Window x:Class="TestWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:view="clr-namespace:TestWPF">
    <Grid>
        <view:colorUserControl />
    </Grid>
</Window>

窗口获取设计时数据:

但不是用户控件:

如何让我的 UserControl 显示设计时数据?

【问题讨论】:

    标签: wpf mvvm


    【解决方案1】:

    您遇到的问题是您实际上并没有使用设计时数据,即使在您的主窗口中也是如此。


    使用设计时数据时,您有两种选择:

    1. DesignInstance - 这用于帮助塑造您的数据上下文。它将通过绑定路径为您提供智能感知支持。
    2. DesignData - 这将让您选择一个 xaml 资源,该资源使用实际示例数据表示您的数据上下文。

    很遗憾,您只能选择一个(它们不能共存)。


    下面列出了一些不错的资源:

    【讨论】:

    • 谢谢!我的代码的第一个问题是我丢失了:xmlns:local="clr-namespace:TestWPF" d:DataContext="{d:DesignInstance Type=local:colorViewModel, IsDesignTimeCreatable=True}"`。第二个问题(我花了一点时间才找到)是我的 ViewModel 没有获取它的属性。换句话说,我需要更改 public List colorList;到公共列表 colorList { 获取;放; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 2013-01-02
    • 1970-01-01
    • 2021-06-28
    • 2011-08-02
    相关资源
    最近更新 更多