【发布时间】: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 显示设计时数据?
【问题讨论】: