在XAML中声明性地定义数据源

本文关键字:定义 数据源 声明 XAML | 更新日期: 2023-09-27 18:10:30

我有一些带有小型静态数据源的列表控件。例如:

<ItemsControl ItemsSource="{Binding Countries}" .../>

和我的视图模型填充列表:

this.Countries.Add(new Country { Code = "BE", Name = "Belgium" });
this.Countries.Add(new Country { Code = "CA", Name = "Canada" });
// etc.

是否有另一种方法来定义列表内容在XAML代替?比如:

<ItemsControl>
    <ItemsControl.ItemsSource>
        <somenamespace:list>
            <mynamespace:Country Code="BE" Name="Belgium" />
            etc.
        </somenamespace:list>
    </ItemsControl.ItemsSource>
</ItemsControl>

我实际上会把列表放在单独的资源文件中,并希望在将它们定义为资源后执行ItemsSource="{StaticResource myListOfCountries}"

我想这样做,以减轻我的虚拟机的样板代码。我想知道它是否会对性能产生负面影响,因为这些对象可以在视图渲染之前创建,而我可以稍后加载这些对象(在导航到,在视图加载,…vs contructor)。欢迎提出任何想法!

在XAML中声明性地定义数据源

您可以创建一个新的CollectionType,然后在XAML中填充它。

,

将在XAML中使用的CollectionType:

using System.Collections.ObjectModel;
namespace WpfApplication4
{
    public class CountryCollection : ObservableCollection<Country>
    {
    }
}

少:

using System;
using System.Collections;
namespace WpfApplication4
{
    public class Country 
    {
        public String Name { get; set; }
        public String Code { get; set; }
    }
}

XAML:

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication4"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:CountryCollection x:Key="CountryList">
            <local:Country Name="Canada" Code="CA"/>
            <local:Country Name="United States" Code="US"/>
            <local:Country Name="Belgium" Code="BE"/>
        </local:CountryCollection>
    </Window.Resources>
    <Grid>
        <ItemsControl ItemsSource="{StaticResource CountryList}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="{Binding Name}"/>
                        <Label Content="{Binding Code}"/>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>
注意,提供的XAML类似于:
    var CountryList = new ObservableCollection<Country>
    {
        new Country {Name = "Canada", Code = "CA"},
        new Country {Name = "United States", Code = "US"},
        new Country {Name = "Belgium", Code = "BE"}
    };

Edit (Update using an ArrayList)

对于在XAML中定义的Collections名称空间,您可以使用
   xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
<Window.Resources>
    <collections:ArrayList x:Key="CountryList">
        <local:Country Name="Canada" Code="CA"/>
        <local:Country Name="United States" Code="US"/>
        <local:Country Name="Belgium" Code="BE"/>
    </collections:ArrayList>
</Window.Resources>

您可以这样做纯XAML:

<Window x:Class="WpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow"
        Height="350"
        Width="525">
    <Window.Resources>
        <XmlDataProvider x:Key="MockList"
                         XPath="/MockObjects/*">
            <x:XData>
                <MockObjects xmlns="">
                    <MockObject  Code="BE"
                                 Name="Belgium" />
                    <MockObject  Code="CA"
                                 Name="Canada" />
                    <MockObject  Code="US"
                                 Name="USA! USA!" />
                </MockObjects>
            </x:XData>
        </XmlDataProvider>
    </Window.Resources>
    <Grid DataContext="{Binding Source={StaticResource MockList}}">
        <ItemsControl ItemsSource="{Binding Mode=Default, XPath=/MockObjects/MockObject}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding XPath=@Code}" FontWeight="Bold" Margin="0 0 5 0"/>
                        <TextBlock Text="{Binding XPath=@Name}" />
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>

我用这个SO的答案作为参考。