WPF Grid background ImageBrush ImageSource via image url in MVVM

private void GenerateImgaBrushImageSourceViaImgUrl(string imgUrlValue)
{
    BitmapImage bmi = new BitmapImage();
    bmi.BeginInit();
    bmi.UriSource = new Uri(imgUrlValue, UriKind.RelativeOrAbsolute);
    bmi.EndInit();
    if (bmi.CanFreeze)
    {
        bmi.Freeze();
    }
    ImgBrush.ImageSource = bmi;
    ImgBrush.Stretch = Stretch.Uniform;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

//xaml
<Window x:Class="WpfApp21.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:behavior="http://schemas.microsoft.com/xaml/behaviors"
        WindowState="Maximized"
        xmlns:local="clr-namespace:WpfApp21"
        mc:Ignorable="d"
        Title="{Binding ImgUrl,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Height="450" Width="800">
    <behavior:Interaction.Triggers>
        <behavior:EventTrigger EventName="KeyDown">
            <behavior:CallMethodAction MethodName="Window_KeyDown" TargetObject="{Binding}"/>
        </behavior:EventTrigger>
    </behavior:Interaction.Triggers>
    <Window.DataContext>
        <local:MainVM/>
    </Window.DataContext>
    <Grid>
        <Border Background="{Binding ImgBrush,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</Window>



//xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.ComponentModel;

namespace WpfApp21
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class MainVM: INotifyPropertyChanged
    { 
        public MainVM()
        {
            ImgBrush = new ImageBrush();
            InitImgBrush();
        }

        private void InitImgBrush()
        {
            imgsList =new List<string>(Directory.GetFiles(@"../../Images"));
            if (imgsList != null && imgsList.Count() > 0)
            {
                ImgUrl = imgsList[0];
                GenerateImgaBrushImageSourceViaImgUrl(ImgUrl);
            }
        }

        private void GenerateImgaBrushImageSourceViaImgUrl(string imgUrlValue)
        {
            BitmapImage bmi = new BitmapImage();
            bmi.BeginInit();
            bmi.UriSource = new Uri(imgUrlValue, UriKind.RelativeOrAbsolute);
            bmi.EndInit();
            if (bmi.CanFreeze)
            {
                bmi.Freeze();
            }
            ImgBrush.ImageSource = bmi;
            ImgBrush.Stretch = Stretch.Uniform;
        }

        public void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key==Key.Down)
            {
                if(++ImgIdx>=imgsList.Count)
                {
                    ImgIdx = 0;
                }
            }
            else if(e.Key==Key.Up)
            {
                if(--ImgIdx<0)
                {
                    ImgIdx = imgsList.Count-1;
                }
            }
            ImgUrl=imgsList[ImgIdx];
            GenerateImgaBrushImageSourceViaImgUrl(ImgUrl);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if(handler!=null)
            {
                handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private List<string> imgsList { get; set; }
        private int imgIdx = 0;
        public int ImgIdx
        {
            get
            {
                return imgIdx;
            }
            set
            {
                if(value!=imgIdx)
                {
                    imgIdx = value;
                    OnPropertyChanged(nameof(ImgIdx));
                }
            }
        }

        private ImageBrush imgBrush;
        public ImageBrush ImgBrush
        {
            get
            {
                return imgBrush;
            }
            set
            {
                if(value!=imgBrush)
                {
                    imgBrush = value;
                    OnPropertyChanged(nameof(ImgBrush));
                }
            }
        }

        private string imgUrl;
        public string ImgUrl
        {
            get
            {
                return imgUrl;
            }
            set
            {
                if (value != imgUrl)
                {
                    imgUrl = value;
                    OnPropertyChanged(nameof(ImgUrl));
                }
            }
        }
    }
}

 

posted @ 2024-10-11 00:23  FredGrit  阅读(6)  评论(0编辑  收藏  举报