首页 > 其他分享 >WPF ListBox thumbnails and image mvvm behavior CallMethodAction

WPF ListBox thumbnails and image mvvm behavior CallMethodAction

时间:2024-05-22 14:32:44浏览次数:25  
标签:CallMethodAction thumbnails mvvm Windows void System private using public

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

 

 

标签:CallMethodAction,thumbnails,mvvm,Windows,void,System,private,using,public
From: https://www.cnblogs.com/Fred1987/p/18206162

相关文章

  • WPF mvvm find element by name
    Copyfrom https://stackoverflow.com/questions/636383/how-can-i-find-wpf-controls-by-name-or-type //xaml<Windowx:Class="WpfApp102.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmln......
  • Community Mvvm Toolkit常用组件的基本使用(第一版)
    一、组件ObservableObjectObservableObject实现了INotifyPropertyChanged和INotifyPropertyChanging,并触发PropertyChanged和PropertyChanging事件1publicclassUser:ObservableObject2{3privatestringname;45publicstringName6{7......
  • [C#] [WPF] 在MVVM中实现拖拽功能 - 入门
    拖拽功能是使用频率较高的一种交互,用传统方法去写很简单,但是在mvvm规则下,也没必要弄得很麻烦我的入门案例就选择了使用mvvm重写tutorialspoint-Interaction里的调色盘案例,view如下MainWindow.xaml这里的重点是控件要允许拖拽以及对应的事件目标控件,填充色绑定,......
  • 手写MVVM
    internalclassDelegateCommand:ICommand{publiceventEventHandler?CanExecuteChanged{add{CommandManager.RequerySuggested+=value;}remove{CommandManager.RequerySuggested-=value;}}publicDelegateComm......
  • WPF Behavior Interaction Triggers EventTrigger EventName CallMethodAction Target
    //xaml<Windowx:Class="WpfApp92.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • WPF MVVM Datagrid Selected Multiple items via behavior interaction.trigger,event
    1.Install Microsoft.Xaml.Behaviors.WpffromNuget;2.Addbehaviorreferenceinxamlxmlns:behavior="http://schemas.microsoft.com/xaml/behaviors"3.Passmethodtomvvmviabehavior,interaction,trigger,eventname,TargetObject,MethodNameinxaml......
  • WPF pass event method to viewmodel via Interaction:CallMethodAction,TargetObject
    <Windowx:Class="WpfApp71.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • WPF控件:密码框绑定MVVM
    以下是一种使用MVVM模式的方法:首先,在ViewModel中添加一个属性来保存密码,我们可以使用SecureString类型。//密码变量privateSecureString_password;//密码属性,用于获取和设置密码publicSecureStringPassword{get{return_passw......
  • WPF关联Mvvm
    WPF在不使用任何框架去关联View和ViewModel的时候,最常用的2种写法是this.DataContext=newMainViewModel();或者<Window.DataContext><viewModels:MainWindowViewModel/></Window.DataContext>而之所以使用模板不起作用,是因为模板是针对UserControl的,例如<DataT......
  • WPF 使用CommunityToolkit.Mvvm进行快速开发
    一、Net框架情况下:NuGet安装CommunityToolkit.Mvvm使用框架可以简洁快速的编辑代码MvvmFoundationViewModel.cs文件内MvvmFoundationViewModel继承ObservableObject属性上添加[ObservableProperty]属性名称第一个字母不要大写,框架会自动生成大写的字段点击查看代码......