首页 > 其他分享 >WPF datagrid datagridtemplatecolumn image mouseenter show the image in big window

WPF datagrid datagridtemplatecolumn image mouseenter show the image in big window

时间:2024-09-10 20:24:13浏览次数:1  
标签:string datagridtemplatecolumn show Windows image System ImageTbk using public

 <DataGridTemplateColumn>
     <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
             <Image Source="{Binding ImgUrl}" Width="200" Height="500"> 
                 <behavior:Interaction.Triggers>
                     <behavior:EventTrigger EventName="MouseEnter">
                         <behavior:InvokeCommandAction 
                             Command="{Binding DataContext.MECmd,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"
                             CommandParameter="{Binding ImgUrl}"/>
                     </behavior:EventTrigger>
                 </behavior:Interaction.Triggers>
             </Image>
         </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
 </DataGridTemplateColumn>

 private void MECmdExecuted(object obj)
 {
     string fullPath = System.IO.Path.GetFullPath(obj?.ToString());
     if(System.IO.File.Exists(fullPath))
     {
         ImageTbk img = new ImageTbk();
         img.ImgSource=fullPath;
         img.ImgSourceStr=fullPath;
         img.ShowDialog();
     }
 }

 

 

 

 

 

 

//usercontrol
<Window x:Class="WpfApp350.ImageTbk"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApp350"
             mc:Ignorable="d"
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Image Source="{Binding ImgSource,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
        <TextBlock Text="{Binding ImgSourceStr,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                   Panel.ZIndex="2"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Bottom"
                   FontSize="30" FontWeight="ExtraBold"/>
    </Grid>
</Window>


//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;

namespace WpfApp350
{
    /// <summary>
    /// Interaction logic for ImageTbk.xaml
    /// </summary>
    public partial class ImageTbk : Window
    {
        public ImageTbk()
        {
            InitializeComponent();
            this.DataContext = this;
        }



        public string ImgSource
        {
            get { return (string)GetValue(ImgSourceProperty); }
            set { SetValue(ImgSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ImgSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ImgSourceProperty =
            DependencyProperty.Register("ImgSource", typeof(string), 
                typeof(ImageTbk), new PropertyMetadata(""));




        public string ImgSourceStr
        {
            get { return (string)GetValue(ImgSourceStrProperty); }
            set { SetValue(ImgSourceStrProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ImgSourceStr.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ImgSourceStrProperty =
            DependencyProperty.Register("ImgSourceStr", typeof(string), 
                typeof(ImageTbk), new PropertyMetadata(""));


    }
}

 

//xaml
<Window x:Class="WpfApp350.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:WpfApp350"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:BookVM/>
    </Window.DataContext>
    <Grid>
        <DataGrid ItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                  SelectedItem="{Binding SelectedBk,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                  VirtualizingPanel.CacheLength="100"
                  EnableRowVirtualization="True"
                  EnableColumnVirtualization="True"
                  CanUserAddRows="False"
                  AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Id" Width="150" Binding="{Binding Id}"/>
                <DataGridTextColumn Header="Name" Width="150" Binding="{Binding Name}"/>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding ImgUrl}" Width="200" Height="500"> 
                                <behavior:Interaction.Triggers>
                                    <behavior:EventTrigger EventName="MouseEnter">
                                        <behavior:InvokeCommandAction 
                                            Command="{Binding DataContext.MECmd,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"
                                            CommandParameter="{Binding ImgUrl}"/>
                                    </behavior:EventTrigger>
                                </behavior:Interaction.Triggers>
                            </Image>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>


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

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

        private void CallMethodAction_DpiChanged(object sender, DpiChangedEventArgs e)
        {

        }
    }

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

        public BookVM()
        {
            InitData();
            InitCommands();
        }

        private void InitCommands()
        {
            MECmd = new DelCmd(MECmdExecuted);
        }

        private void MECmdExecuted(object obj)
        {
            string fullPath = System.IO.Path.GetFullPath(obj?.ToString());
            if(System.IO.File.Exists(fullPath))
            {
                ImageTbk img = new ImageTbk();
                img.ImgSource=fullPath;
                img.ImgSourceStr=fullPath;
                img.ShowDialog();
            }
        }

        private void InitData()
        {
            BooksCollection = new ObservableCollection<Book>();
            var imgsList = System.IO.Directory.GetFiles(@"../../Images");
            int imgsCnt = imgsList.Count();
            for(int i=0;i<10000;i++)
            {
                BooksCollection.Add(new Book()
                {
                    Id = i + 1,
                    Name = $"Name_{i + 1}",
                    ImgUrl = imgsList[i % imgsCnt]
                });
            }
        }

        private ObservableCollection<Book> booksCollection;
        public ObservableCollection<Book> BooksCollection
        {
            get
            {
                return booksCollection;
            }
            set
            {
                if (value != booksCollection)
                {
                    booksCollection = value;
                    OnPropertyChanged(nameof(BooksCollection));
                }
            }
        }

        public void Image_MouseEnter(object sender, MouseEventArgs e)
        {

        }

        public DelCmd MECmd { get; set; }
    }


    public class Book
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string ImgUrl { get; set; }
    }

    public class DelCmd : ICommand
    {
        public event EventHandler CanExecuteChanged
        {
            add
            {
                CommandManager.RequerySuggested += value;
            }
            remove
            {
                CommandManager.RequerySuggested -= value;
            }
        }

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

 

标签:string,datagridtemplatecolumn,show,Windows,image,System,ImageTbk,using,public
From: https://www.cnblogs.com/Fred1987/p/18407122

相关文章

  • 论文笔记--See through Gradients. Image Batch Recovery via GradInversion
    SeethroughGradients.ImageBatchRecoveryviaGradInversion\(W^{FC}\in\mathbb{R}^{M\timesN}\),其输入为一个M维向量\(v\in\mathbb{R}^M\),\(\DeltaW^{FC}_{m,n,k}\)是损失函数对全连接层\(W\)的导数。对于一个特定的类别\(n\),(\(z\)为全连接层输出的logits),其......
  • 论文精读-U-KAN Makes Strong Backbone for Medical Image Segmentation and Generati
    论文链接:https://arxiv.org/abs/2406.02918 论文代码:https://yes-u-kan.github.io/一、参考文献[1]LiC,LiuX,LiW,etal.U-KANMakesStrongBackboneforMedicalImageSegmentationandGeneration[J].arXivpreprintarXiv:2406.02918,2024.[2]LiuZ,Wan......
  • GEE错误:Image.select: Band pattern ‘BQA‘ did not match any bands. Available ban
    目录错误原始代码Landsat8TOA数据介绍错误解析正确的代码 结果错误Errorinmap(ID=LC08_044034_20130603):Image.select:Bandpattern'BQA'didnotmatchanybands.Availablebands:[B1,B2,B3,B4,B5,B6,B7,B8,B9,B10,B11,QA_PIXEL,QA_RADSAT......
  • 智能提取:OfficeImagesExtractor让文档图片提取更简单
    “科技是国之利器,也是民之福祉。”在数字化办公日益普及的今天,我们对文档处理的需求也在不断增长。尤其是对于Office文档中的图片、视频和音频等多媒体内容的提取,传统的方法是繁琐且效率低下的。在这样的背景下,一款能够高效、高质量提取Office文档中多媒体内容的工具显得尤为......
  • WPF DataGridTemplateColumn.CellTemplate Command CommandParameter
    <DataGridTemplateColumnHeader="Image"><DataGridTemplateColumn.CellTemplate><DataTemplate><ButtonCommand="{BindingDataContext.EnterCmd,RelativeSource={RelativeSourceFindAn......
  • v-if和v-show的区别
    v-if和v-show是Vue.js中常用的指令,用于根据条件来控制元素的显示和隐藏。它们的区别主要体现在以下几个方面:渲染方式:v-if是基于条件进行的“惰性渲染”,即只有在条件为真时才会渲染对应的组件或元素,而在条件为假时会直接移除对应的组件或元素。这意味着在条件为假时,相关的组件或......
  • 使用Blip的预训练好的imageEncoder并替换其textDecoder
    fromtransformersimportBlipProcessor,BlipTextConfigfromtransformers.models.blip.modeling_blip_textimportBlipTextLMHeadModelfromtransformersimportAutoTokenizermodel=BlipForConditionalGeneration.from_pretrained("huggingface.co/Salesforc......
  • ctfshow web13
     尝试 常规姿势上传文件打开网站初步判定为文件上传漏洞。随便选择几个文件上传,提示错误不选择任何文件直接点提交也会报错打开burpsuite抓包,改掉MIME类型,也就是图示位置,发现也不行,应该不是MIME过滤一头雾水,只能换个思路。 -------------------------......
  • ctfshow web红包题第二弹题解
    从今天开始记录刷题日常打开靶场平平无奇,看源代码发现如下提示get方式提交cmd参数,猜测是命令执行漏洞,先写个phpinfo();试试。有用,但报错cerror查看显示出来部分php代码,过滤了很多东西if(preg_match("/[A-Za-oq-z0-9$]+/",$cmd)) 第一个正则表达式把字母数字几乎全......
  • onShow执行顺序以及和onHide的对比
    onshow的介绍onShow方法是在小程序启动或从后台进入前台时触发的方法。onShow方法的主要作用是监听用户的行为并做出相应的响应,比如在小程序启动时展示欢迎页、在用户进入小程序时更新数据等。开发者可以通过重写onShow方法来编写自己的业务逻辑。onshow的顺序进页面的话是......