如何在XAML设计数据文件中两次(或更频繁地)引用同一对象?我尝试使用{x:Reference},但这似乎不起作用.
这是一个例子:
样本数据网格第二列单元格中的组合框显示"数据类型"列表.可用数据类型列表来自Types主窗口视图模型的属性(=数据上下文).网格中的项目列表来自Items视图模型的属性.每个项目都有一个列Name和一个Type列,其中Type引用数据类型对象.
示例网格如下所示:

下面是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>
Run Code Online (Sandbox Code Playgroud)
上面,我{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>
Run Code Online (Sandbox Code Playgroud)
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
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; }
}
}
Run Code Online (Sandbox Code Playgroud)
为什么x:Reference不起作用的背景......
x:Reference是 XAML 2009 的一项功能。
您不能x:Reference在根据 MSDN 文档编译的 XAML 标记中使用。
它是为松散的 XAML 设计的...例如,如果您创建 XAML 页面 (.xaml) 并通过 Internet Explorer 加载它。
\n\n当您使用 DesignData 时,Designer 会有效地创建并编译一个新类,其形状和内容如 DesignData 文件中所述。
\n\nVisual Studio/Blend Designers 中不支持它。
\n\n\n\n这是一个反驳论据。
\n\n\n\n\n\n\n以下是 Adam Nathan 的《WPF 4 unleashed》一书中的解释:“\nx:Reference 标记扩展经常被错误地与 \n XAML2009 功能关联,\n 在撰写本文时,\n 这些功能只能从松散的 XAML 使用。尽管 x:Reference 是 WPF 4 中的一项新功能,但只要您的项目面向 .NET Framework 版本 4 或更高版本,\n 就可以从 XAML2006 开始使用它。\n 一个问题是 XAML 设计器\n在 Visual Studio 2010 中,\xc3\xa2\xef\xbf\xbd\xef\xbf\xbdt 无法正确处理\nx:Reference,因此它会给出以下设计时错误,您可以\n 安全地忽略该错误:服务提供程序缺少INameResolver 服务”
\n
解决办法...
\n| 归档时间: |
|
| 查看次数: |
3573 次 |
| 最近记录: |