WPF ListBox thumbnails and image mvvm behavior CallMethodAction

<Window x:Class="WpfApp108.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"
        xmlns:local="clr-namespace:WpfApp108"
        mc:Ignorable="d" WindowState="Maximized"
        Title="{Binding SelectedItem,ElementName=lbx}" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ListBox Grid.Column="0" x:Name="lbx"  SelectedIndex="0"
                 ItemsSource="{Binding ImgsList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
            <behavior:Interaction.Triggers>
                <behavior:EventTrigger EventName="SelectionChanged">
                    <behavior:CallMethodAction TargetObject="{Binding}" MethodName="lbx_SelectionChanged" />
                </behavior:EventTrigger>
            </behavior:Interaction.Triggers>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Viewbox Width="200" Height="200">
                        <Image Source="{Binding Content,RelativeSource={RelativeSource AncestorType=ListBoxItem}}"/>
                    </Viewbox>
                </DataTemplate>
            </ListBox.ItemTemplate> 
        </ListBox>
        <Image Grid.Column="1" Source="{Binding SelectedItem,ElementName=lbx}" Stretch="Uniform" RenderOptions.BitmapScalingMode="HighQuality" >
            <behavior:Interaction.Triggers>
                <behavior:EventTrigger EventName="MouseWheel">
                    <behavior:CallMethodAction TargetObject="{Binding}" MethodName="Image_MouseWheel"/>
                </behavior:EventTrigger>
            </behavior:Interaction.Triggers>
            <Image.RenderTransform>
                <ScaleTransform ScaleX="{Binding ScaleLevel,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                ScaleY="{Binding ScaleLevel,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
            </Image.RenderTransform>
        </Image>
    </Grid>
</Window>


//xaml.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Security;
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;

namespace WpfApp108
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var vm = new MainVM();
            this.DataContext= vm; 
        } 
    }

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

        #region Properties
        private double scaleLevel = 1.0;
        public double ScaleLevel
        {
            get
            {
                return scaleLevel;
            }
            set
            {
                if(value!=scaleLevel)
                {
                    scaleLevel = value;
                    OnPropertyChanged(nameof(ScaleLevel));
                }
            }
        }

        private ObservableCollection<string> imgsList;
        public ObservableCollection<string> ImgsList
        {
            get
            {
                return imgsList;
            }
            set
            {
                if(value!=imgsList)
                {
                    imgsList = value;
                    OnPropertyChanged(nameof(ImgsList));
                }
            }
        }

        #endregion

        #region Commands Methods

        public MainVM()
        {
            InitData();
        }

        private void InitData()
        {
            string dir = @"..\..\Images";
            string fullDir = System.IO.Path.GetFullPath(dir);
            if(System.IO.Directory.Exists(fullDir)) 
            { 
                var allFiles=System.IO.Directory.GetFiles(fullDir);
                ImgsList = new ObservableCollection<string>(allFiles);
            }
        }

        public void Image_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (e.Delta > 0)
            {
                ScaleLevel *= 1.2;
            }
            else
            {
                ScaleLevel /= 1.2;
            }
        }

        public void lbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        #endregion
    }

    public class DelCmd : ICommand
    {
        public event EventHandler CanExecuteChanged;
        protected void RaiseCanExecuteChanged()
        {
            var handler = CanExecuteChanged;
            if (handler != null)
            {
                handler?.Invoke(this, EventArgs.Empty);
            }
        }

        private Action<object> _execute;
        private Predicate<object> _canExecute;

        public DelCmd(Action<object> _executeValue, Predicate<object> _canExecuteValue)
        {
            _execute = _executeValue;
            _canExecute = _canExecuteValue;
        }

        public DelCmd(Action<object> _executeValue) : this(_executeValue, null)
        { }



        public bool CanExecute(object parameter)
        {
            if (_canExecute == null)
            {
                return true;
            }
            return _canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            _execute(parameter);
        }
    }
}

 

 

posted @ 2024-05-22 14:22  FredGrit  阅读(4)  评论(0编辑  收藏  举报