C# 在XAML中以声明方式定义数据源

C# 在XAML中以声明方式定义数据源,c#,wpf,xaml,windows-phone-8,C#,Wpf,Xaml,Windows Phone 8,我有带有小型静态数据源的列表控件。例如: <ItemsControl ItemsSource="{Binding Countries}" .../> 是否有替代方法来定义XAML中的列表内容?比如: <ItemsControl> <ItemsControl.ItemsSource> <somenamespace:list> <mynamespace:Country Code="BE" Name

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

<ItemsControl ItemsSource="{Binding Countries}" .../>
是否有替代方法来定义XAML中的列表内容?比如:

<ItemsControl>
    <ItemsControl.ItemsSource>
        <somenamespace:list>
            <mynamespace:Country Code="BE" Name="Belgium" />
            etc.
        </somenamespace:list>
    </ItemsControl.ItemsSource>
</ItemsControl>
实际上,我会将列表放在单独的资源文件中,并希望在将它们定义为资源后执行
ItemsSource=“{StaticResource mylistofccountries}”


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

您可以通过创建一个新的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; }
    }
}
<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:

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; }
    }
}
<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"}
    };
var CountryList=新的ObservableCollection
{
新国家{Name=“Canada”,Code=“CA”},
新国家{Name=“美国”,Code=“美国”},
新国家{Name=“比利时”,Code=“BE”}
};
编辑(使用ArrayList更新)

使用XAML中定义的集合命名空间,可以使用

   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>
xmlns:collections=“clr命名空间:System.collections;assembly=mscorlib”

这是纯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>


我用作参考。

此链接可能对您有所帮助。您不需要在公共类CountryCollection上设置get?不,CountryCollection是从ObservableCollection派生的,因此不需要创建setter/getter。通过继承,它仍然只是拥有ObservableCollection的所有正常功能。见我的编辑谢谢,我会试试这个。有没有办法不必创建新的集合类型?在这种情况下,我甚至不需要ObservableCollection,List就可以了(例如)。不知道是否可以重复使用现有的泛型类型。@是的,如果不打算使用ObservableCollection,则不必创建新类型。您可以简单地使用ArrayList(我在更新中添加了XAML)来代替,但要意识到这种方法破坏了MVVM,因为您的“模型”(
国家
)仅在视图数据中提供。或者,您可以编写一个XML文件,并在ViewModel初始化期间将其解析为对象。