簡體   English   中英

在XAML中以聲明方式定義數據源

[英]Defining data source declaratively in XAML

我有帶有小型靜態數據源的列表控件。 例如:

<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}"

我想這樣做是為了減輕虛擬機中的樣板代碼。 我想知道這是否會對性能產生負面影響,因為這些對象可以在渲染視圖之前創建,而我可以稍后再加載這些對象(在導航到視圖加載時,...在構造時)。 歡迎任何想法!

您可以通過創建一個新的CollectionType,然后在XAML中填充它來做到這一點。

例,

將在XAML中使用的CollectionType:

using System.Collections.ObjectModel;

namespace WpfApplication4
{
    public class CountryCollection : ObservableCollection<Country>
    {
    }
}

POCO:

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"}
    };

編輯(使用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>

我用這個答案作為參考。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM