我目前正在使用列表框ItemsSource来绑定值 来自x:数组如下:
<x:Array x:Key="SampleImages" Type="{x:Type BitmapImage}">
<BitmapImage UriSource="www.google.com/a.png" />
<BitmapImage UriSource="www.google.com/b.jpg" />
<BitmapImage UriSource="www.google.com/c.jpg" />
<BitmapImage UriSource="www.google.com/d.jpg" />
</x:Array>
然后在与
相同的xaml页面中使用它 <ListBox ItemsSource="{StaticResource SampleImages}">
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}"></Image>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
而不是在数组中定义图像, 我想使用数组从另一个资源字典中获取图像(SampleImages.xaml;因此,如果需要,以后更容易更改图像)定义为 -
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<BitmapImage UriSource="www.google.com/a.png" />
<BitmapImage UriSource="www.google.com/b.jpg" />
<BitmapImage UriSource="www.google.com/c.jpg" />
<BitmapImage UriSource="www.google.com/d.jpg" />
</ResourceDictionary>
您如何在x:Array中引用SampleImages.xaml? 在此先感谢。
答案 0 :(得分:1)
将整个Array
移动到资源文件中。
在app.xaml
中引用它以在整个应用程序中可用。
SampleImages.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<x:Array x:Key="SampleImages" Type="{x:Type BitmapImage}">
<BitmapImage UriSource="www.google.com/a.png" />
<BitmapImage UriSource="www.google.com/b.jpg" />
<BitmapImage UriSource="www.google.com/c.jpg" />
<BitmapImage UriSource="www.google.com/d.jpg" />
</x:Array>
</ResourceDictionary>
<强> App.xaml中:强>
<Application x:Class="Project.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml" >
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/YourResourceFolderIfAny/SampleImages.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
任何其他观点:
<ListBox ItemsSource="{StaticResource SampleImages}">
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}"></Image>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>