引用XAML设计数据中的对象

时间:2012-09-07 09:35:04

标签: wpf visual-studio-2010 xaml

如何在XAML设计数据文件中两次(或更频繁地)引用同一对象? 我尝试使用{x:Reference},但这似乎不起作用。

以下是一个例子:

样本数据网格第二列单元格中的组合框显示“数据类型”列表。可用数据类型列表来自主窗口视图模型的Types属性(=数据上下文)。网格中的项目列表来自视图模型的Items属性。每个项目都有NameType列,其中Type引用数据类型对象。

示例网格如下所示:

Screenshot

以下是XAML设计数据,它应该在Visual Studio设计器中显示相同的网格内容(但它没有):

<?xml version="1.0" encoding="utf-8" ?>
<local:MainWindowViewModel
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:DataGridSample"
    >
    <local:MainWindowViewModel.Types>
        <local:DataType Name="String" x:Name="String"/>
        <local:DataType Name="Integer" x:Name="Integer"/>
    </local:MainWindowViewModel.Types>
    <local:MainWindowViewModel.Items>
        <local:Item Name="Lorem" Type="{x:Reference String}"/>
        <local:Item Name="Ipsum" Type="{x:Reference Integer}"/>
    </local:MainWindowViewModel.Items>
</local:MainWindowViewModel>

上面,我使用{x:Reference String}来获取<local:DataType Name="String" x:Name="String"/>创建的对象的引用。

在Visual Studio设计器中,列表为空,并显示错误消息“在标记中找到错误:... DesignData.xaml”。在设计数据XAML文件的编辑器中,我收到错误消息“服务提供程序缺少INameResolver服务”。

我可以在设计数据文件中使用{x:Reference}来替代对象吗?

为了完整性,以下是我的样本的剩余文件:

MainWindow.xaml:

<Window x:Class="DataGridSample.MainWindow"
        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"
        Title="Sample" Height="300" Width="400"
        d:DataContext="{d:DesignData Source=DesignData.xaml}">
    <Window.Resources>
        <CollectionViewSource x:Key="types" Source="{Binding Types}"/>
    </Window.Resources>

    <Grid>
        <DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="*"/>
                <DataGridComboBoxColumn SelectedItemBinding="{Binding Type}"
                                        ItemsSource="{Binding Source={StaticResource types}}" 
                                        DisplayMemberPath="Name"
                                        Header="Type" Width="*"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

MainWindow.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 DataGridSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        readonly MainWindowViewModel _viewModel = new MainWindowViewModel();

        public MainWindow()
        {
            InitializeComponent();

            DataContext = _viewModel;
        }
    }
}

MainWindowViewModel.cs:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace DataGridSample
{
    public class MainWindowViewModel
    {
        private readonly ObservableCollection<DataType> _dataTypes;
        private readonly ObservableCollection<Item> _items;

        public MainWindowViewModel()
        {
            DataType typeString = new DataType {Name = "String"};
            DataType typeInteger = new DataType {Name = "Integer"};

            _dataTypes = new ObservableCollection<DataType> {typeString, typeInteger};
            _items = new ObservableCollection<Item>
                {
                    new Item {Name = "Lorem", Type = typeString},
                    new Item {Name = "Ipsum", Type = typeInteger}
                };
        }

        public ObservableCollection<DataType> Types
        {
            get
            {
                return _dataTypes;
            }
        }

        public ObservableCollection<Item> Items
        {
            get
            {
                return _items;
            }
        }
    }

    public class DataType
    {
        public string Name { get; set; }
    }

    public class Item
    {
        public string Name { get; set; }

        public DataType Type { get; set; }
    }
}

1 个答案:

答案 0 :(得分:1)

x:Reference无效的原因背景.....

x:Reference是XAML 2009功能。

您不能在根据MSDN文档编译的XAML标记中使用x:Reference

它专为松散的XAML而设计......例如。如果您创建XAML页面(.xaml)并通过Internet Explorer加载它。

使用DesignData时,Designer会有效地创建和编译一个新类,其形状和内容如DesignData文件中所述。

Visual Studio / Blend Designers中不支持它。

这是一个反驳论点。

  

以下是Adam Nathan的WPF 4释放书中的解释:“The   x:参考标记扩展通常与错误关联   XAML2009的功能只能在当时的松散XAML中使用   写这篇文章。虽然x:Reference是WPF 4中的一个新功能,但它   只要您的项目是,就可以从XAML2006中使用   定位.NET Framework的版本4或更高版本。一个小故障是   Visual Studio 2010中的XAML设计器无法正确处理   x:参考,因此它可以提供以下设计时错误   安全地忽略:服务提供者缺少INameResolver服务“

解决方案解决方案......