Я бы использовал ListView
и ObservableCollection<string>
во ViewModel. ObservableCollection<string>
содержит динамический список путей к изображениям. Убедитесь, что для параметра «Действие построения изображений» установлено значение «Ресурс». Затем в свойстве Background Window поместите ImageBrush, где вы привязываете свойство Source к свойству SelectedItem со значением ListView
. Путь строк изображений следует схеме, которую вы можете найти здесь: https://docs.microsoft.com/en-us/dotnet/framework/wpf/app-development/pack-uris-in-wpf
По желанию (изображения являются BuildAction к ресурсу и копируются, если новее):
MainWindow.xaml
<Window x:Class="WinTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WinTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<local:TestViewModel x:Key="viewModel"/>
<local:ImageConverter x:Key="converter"/>
</Window.Resources>
<Window.DataContext>
<Binding Source="{StaticResource viewModel}" IsAsync="True"/>
</Window.DataContext>
<Window.Background>
<ImageBrush ImageSource="{Binding SelectedImagePath, Converter={StaticResource converter}}"/>
</Window.Background>
<Grid Background="Transparent">
<ListView Background="Transparent" SelectedValue="{Binding SelectedImagePath, Mode=TwoWay}" ItemsSource="{Binding PathList}"/>
</Grid>
</Window>
TestViewModel.cs (Коллекция может использоваться как строка или список Uri. Вы должны создать новый Uri в Converter из значения, если используете строки)
public class TestViewModel : BasePropertyChangeNotification
{
public ObservableCollection<Uri> PathList
{
get;
private set;
}
public Uri SelectedImagePath
{
get { return this.selectedImagePath; }
set { this.SetProperty(ref this.selectedImagePath, value); }
}
private Uri selectedImagePath = new Uri("pack://application:,,,/Images/img1.jpg", UriKind.RelativeOrAbsolute);
public TestViewModel()
{
this.PathList = new ObservableCollection<Uri>
{
new Uri("pack://application:,,,/Images/img1.jpg", UriKind.RelativeOrAbsolute),
new Uri("pack://application:,,,/Images/img2.jpg", UriKind.RelativeOrAbsolute),
new Uri("pack://application:,,,/Images/img3.jpg", UriKind.RelativeOrAbsolute),
new Uri("pack://application:,,,/Images/img4.jpg", UriKind.RelativeOrAbsolute),
new Uri("pack://application:,,,/Images/img13.jpg", UriKind.RelativeOrAbsolute)
};
}
}
ImageConverter.cs
public class ImageConverter : IValueConverter
{
public object Convert(
object value, Type targetType, object parameter, CultureInfo culture)
{
return new BitmapImage(value as Uri);
}
public object ConvertBack(
object value, Type targetType, object parameter, CultureInfo culture)
{
return Binding.DoNothing;
}
}
Вот и все.