首页 > 其他分享 >WPF datagrid contextmenu menuitem commandparameter CommandParameter="{Binding RelativeSource={

WPF datagrid contextmenu menuitem commandparameter CommandParameter="{Binding RelativeSource={

时间:2024-09-14 11:15:10浏览次数:1  
标签:jsonStr PlacementTarget commandparameter items RelativeSource dg var null

Install-package newtonsoft.json

 

 

 <DataGrid.ContextMenu>
     <ContextMenu>
         <MenuItem Header="Export Selected" Command="{Binding ExportSelectedCmd}"
    CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
             AncestorType=ContextMenu},Path=PlacementTarget}"/>
         <MenuItem Header="Export All" Command="{Binding ExportAllCmd}"
    CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
             AncestorType=ContextMenu},Path=PlacementTarget}"/>
     </ContextMenu>
 </DataGrid.ContextMenu>           
 <DataGrid.Columns>



//viewmodel
public DelCmd ExportSelectedCmd { get; set; }
public DelCmd ExportAllCmd { get; set; }

private void InitCmds()
{
    ExportSelectedCmd = new DelCmd(ExportSelectedCmdExecuted);
    ExportAllCmd = new DelCmd(ExportAllCmdExecuted);
}

private void ExportAllCmdExecuted(object obj)
{
    var dg = obj as DataGrid;
    if(dg!=null)
    {
        var items = dg.ItemsSource.Cast<Book>()?.ToList();
        if(items!=null && items.Any())
        {
            string jsonStr=JsonConvert.SerializeObject(items,formatting: Formatting.Indented);
            string allJsonFile = $"All{DateTime.Now.ToString("yyyyMMddHHmmssffff")}_{Guid.NewGuid().ToString()}.json";
            File.AppendAllText(allJsonFile, jsonStr);
            MessageBox.Show($"Exported at:\n{allJsonFile}", "Export successfully!", MessageBoxButton.OK);
        }
    }
}

private void ExportSelectedCmdExecuted(object obj)
{
    var dg = obj as DataGrid;
    if (dg != null)
    {
        var items = dg.SelectedItems.Cast<Book>()?.ToList();
        if (items != null && items.Any())
        {
            string jsonStr = JsonConvert.SerializeObject(items, formatting: Formatting.Indented);
            string selectedFile = $"Selected{DateTime.Now.ToString("yyyyMMddHHmmssffff")}_{Guid.NewGuid().ToString()}.json";
            File.AppendAllText(selectedFile, jsonStr);
            MessageBox.Show($"Exported at:\n{selectedFile}", "Export successfully!", MessageBoxButton.OK);
        }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

标签:jsonStr,PlacementTarget,commandparameter,items,RelativeSource,dg,var,null
From: https://www.cnblogs.com/Fred1987/p/18413573

相关文章

  • WPF DataGridTemplateColumn.CellTemplate Command CommandParameter
    <DataGridTemplateColumnHeader="Image"><DataGridTemplateColumn.CellTemplate><DataTemplate><ButtonCommand="{BindingDataContext.EnterCmd,RelativeSource={RelativeSourceFindAn......
  • WPF ContextMenu MVVM Command CommandParameter Path=PlacementTarget
    <DataGridItemsSource="{BindingBooksList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"SelectionMode="Extended"><DataGrid.ContextMenu><ContextMenu><MenuItemHeader="Ex......
  • WPF Behavior InvokeCommandAction Command CommandParameter
    //xaml<behavior:Interaction.Triggers><behavior:EventTriggerEventName="MouseWheel"SourceObject="{BindingElementName=img}"><behavior:InvokeCommandActionCommand="{BindingMouseWheelCmd}"......
  • 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......
  • WPF relativesource,self,FindAncestor,AncestorType,AncestorLevel,PreviousData,Tem
    <Windowx:Class="WpfApp68.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......
  • WPF 解决 CommandParameter 参数不更新问题
    参考https://devbox.cn/p/WPFCommandParame_71b81418.html环境软件/系统版本说明WindowsWindows10专业版22H219045.4046MicrosoftVisualStudioMicrosoftVisualStudioCommunity2022(64位)-17.6.5Microsoft.NetSDK8.0.101手动安装Mic......
  • RelativeSource
    RelativeSourceSelfTemplateParentAscsterTypeRelativeSource和ElementName的区别RelativeSource必须是父级控件,它是沿着可视化树向上查找。ElementName不需要,同一级控件也可以。具体原因学习NameScope就行了。Mode=Self数据源是作为目标依赖属性所属的实例。下面的例......
  • Binding 中 Elementname,Source,RelativeSource 三种绑定的方式
    在WPF应用的开发过程中Binding是一个非常重要的部分。在实际开发过程中Binding的不同种写法达到的效果相同但事实是存在很大区别的。这里将实际中碰到过的问题做下汇总记录和理解。1. source= {binding}和source={bindingRelativeSource={RelativeSourceself},Path=Dat......
  • WPF教程(四)RelativeSource属性
    我们进行Bingding时,如果明确知道数据源的Name,就能用Source或者ElementName进行绑定,但是有时候我们需要绑定的数据源可能没有明确的Name,此时我们就需要利用Bingding的RelativeSource进行绑定,这种办法的意思是指当前元素和绑定源的位置关系。(1)控件关联自身的属性——Self <Windowx:......
  • WPF 使用 StaticResource、DynamicResource、RelativeSource
    StaticResource(静态资源)依赖属性静态资源在第一次编译后即确定其对象或值,之后不能对其进行修改。StaticResources的适用场合:(1)在资源第一次引用之后无需再修改资源的值。(2......