首页 > 其他分享 >WPF MenuItem behavior MVVM

WPF MenuItem behavior MVVM

时间:2024-07-06 20:30:13浏览次数:17  
标签:Files string MVVM SelectedBook bytes File MenuItem WPF sfd

//xaml 
<Image Grid.Column="1" ClipToBounds="True"  
        Source="{Binding SelectedItem.ImgUrl,ElementName=lbx}">
     <Image.ContextMenu>
         <ContextMenu> 
             <MenuItem  Header="Save As Picture">
                 <behavior:Interaction.Triggers>
                     <behavior:EventTrigger EventName="Click">
                         <behavior:CallMethodAction TargetObject="{Binding}" MethodName="SaveAsImageClick"/>
                     </behavior:EventTrigger>
                 </behavior:Interaction.Triggers>
             </MenuItem> 
             <MenuItem Header="Save As String">
                 <behavior:Interaction.Triggers>
                     <behavior:EventTrigger EventName="Click">
                         <behavior:CallMethodAction TargetObject="{Binding}" MethodName="SaveAsStringClick"/>
                     </behavior:EventTrigger>
                 </behavior:Interaction.Triggers>
             </MenuItem>
         </ContextMenu>
     </Image.ContextMenu>


//cs
public void SaveAsImageClick(object sender,EventArgs e)
{
    SaveFileDialog sfd = new SaveFileDialog();
    sfd.Filter = "Image Files|*.jpg;*.png;|All Files|*.*";
    if (sfd.ShowDialog() == true && SelectedBook != null)
    {
       using(FileStream fs=new FileStream(sfd.FileName,FileMode.OpenOrCreate))
        {
            var bytes = File.ReadAllBytes(SelectedBook.ImgUrl);
            fs.Write(bytes,0,bytes.Count());
        }
    }
}

public void SaveAsStringClick(object sender,EventArgs e)
{ 
    SaveFileDialog sfd = new SaveFileDialog();
    sfd.Filter = "Text Files|*.txt*|All Files|*.*";
    if(sfd.ShowDialog()==true && SelectedBook!=null)
    {
        var imgUrl = System.IO.Path.GetFullPath(SelectedBook.ImgUrl);
        string str = ConvertImgToString(imgUrl);
        File.WriteAllText(sfd.FileName, str);
    } 
}

private string ConvertImgToString(string imgFullUrl)
{ 
    if(File.Exists(imgFullUrl))
    {
        byte[] bytes = File.ReadAllBytes(imgFullUrl);
        string base64Str = Convert.ToBase64String(bytes);
        return base64Str;
    }
    return string.Empty;
} 

 

标签:Files,string,MVVM,SelectedBook,bytes,File,MenuItem,WPF,sfd
From: https://www.cnblogs.com/Fred1987/p/18287698

相关文章

  • WPF MVVM capture window keyboard
    //xaml<behavior:Interaction.Triggers><behavior:EventTriggerEventName="KeyDown"><behavior:CallMethodActionTargetObject="{Binding}"MethodName="Window_KeyDown"/></beha......
  • WPF自定义控件与样式-自定义按钮(Button)
    一、前言程序界面上的按钮多种多样,常用的就这几种:普通按钮、图标按钮、文字按钮、图片文字混合按钮。本文章记录了不同样式类型的按钮实现方法。二、固定样式的按钮固定样式的按钮一般在临时使用时或程序的样式比较固定时才会使用,按钮整体样式不需要做大的改动。2.1普通按钮-......
  • WPF Menu实现快捷键操作
    很多小伙伴说,在Menu中,实现单个快捷键操作很简单,怎么实现多个快捷键操作和,组合快捷键呢,今天他来了。上代码和效果图一、Ctrl+Shift+任意子母键实现快捷键组合<Windowx:Class="XH.TemplateLesson.MenuWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xa......
  • WPF DataContext
    后台代码:publicclassStudent{publicintId{get;set;}publicstringName{get;set;}publicintAge{get;set;}} 前台代码:<Windowx:Class="BindingTest.MainWindow"xmlns="http://schem......
  • 学懂C#编程:WPF应用开发系列——WPF之ComboBox控件的详细用法
    WPF(WindowsPresentationFoundation)中的ComboBox控件是一个下拉列表控件,允许用户从一组预定义的选项中选择一个选项。以下是ComboBox控件的详细用法,并附带示例说明。ComboBox的基本用法1.XAML定义:在XAML中定义一个ComboBox控件,并添加一些选项。<Windowx:Class="ComboBox......
  • WPF Performance Suite, Microsoft Windows Performance Toolkit
    Copyfrom https://www.cnblogs.com/lindexi/p/12086719.htmlhttps://learn.microsoft.com/en-us/previous-versions/aa969767(v=vs.110) 1.Downloadurl:  https://download.microsoft.com/download/A/6/A/A6AC035D-DA3F-4F0C-ADA4-37C8E5D34E3D/setup/WinSDKPerformanceT......
  • WPF Datagrid ContextMenu MenuItem Command CommandParameter MultiBinding
     //xaml<Windowx:Class="WpfApp194.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas......
  • 推荐一款Win11主题WPF UI框架
    最近在微软商店,官方上架了新款Win11风格的WPF版UI框架【WPFGalleryPreview1.0.0.0】,这款应用引入了前沿的FluentDesignUI设计,为用户带来全新的视觉体验。WPFGallery简介做为一关注前沿资讯的开发人员,首先关注的是应用WPFGallery到我们的程序中,需要具备的条件:.N......
  • WPF AllowsTransparency DragMove
     //xaml<Windowx:Class="WpfApp191.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas......
  • WPF open image and print as pdf file
    //xaml<Windowx:Class="WpfApp189.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......