WPF usercontrol listbox mvvm contextmenu memuitem command commandparameter
//xaml <UserControl x:Class="WpfApp155.UCLbx" 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:WpfApp155" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <Grid> <ListBox ItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="{Binding ActualWidth,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}" Height="{Binding ActualHeight,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling" > <ListBox.ItemTemplate> <DataTemplate> <Grid TextBlock.FontSize="100" TextBlock.Foreground="Red" Width="{Binding ActualWidth,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}" Height="{Binding ActualHeight,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"> <Grid.Background> <ImageBrush ImageSource="{Binding DataContext.ImgSource,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}}" Stretch="Uniform"/> </Grid.Background> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding DataContext.Id,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}}"/> <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding DataContext.Name,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}}"/> <TextBlock Grid.Row="0" Grid.Column="2" Text="{Binding DataContext.Topic,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}}"/> <TextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Text="{Binding DataContext.ISBN,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}}"/> </Grid> </DataTemplate> </ListBox.ItemTemplate> <ListBox.ContextMenu> <ContextMenu> <MenuItem Header="Export"> <MenuItem Header="Export CSV" Command="{Binding ExportCSVCommand}" CommandParameter="{Binding Path=PlacementTarget,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ContextMenu}}}"/> </MenuItem> </ContextMenu> </ListBox.ContextMenu> </ListBox> </Grid> </UserControl> //cs using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.IO; 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; using Newtonsoft.Json; namespace WpfApp155 { /// <summary> /// Interaction logic for UCLbx.xaml /// </summary> public partial class UCLbx : UserControl { public UCLbx() { InitializeComponent(); var vm = new UCLbxVM(); this.DataContext = vm; } } public class UCLbxVM : INotifyPropertyChanged { public UCLbxVM() { InitBooksCollection(); InitCommands(); } private void InitCommands() { ExportCSVCommand = new DelCommand(ExportCSVCommandExecuted); } private void ExportCSVCommandExecuted(object? obj) { var lbx = obj as ListBox; if (lbx != null) { var jsonStr = JsonConvert.SerializeObject(lbx.ItemsSource, Formatting.Indented); string jsonFile = $"Json_{DateTime.UtcNow.ToString("yyyyMMddHHmmssffff")}.json"; File.WriteAllText(jsonFile, jsonStr); MessageBox.Show(jsonFile, "Saved in Json"); } } public DelCommand ExportCSVCommand { get; set; } private void InitBooksCollection() { BooksCollection = new ObservableCollection<Book>(); var imgs = Directory.GetFiles(@"../../../Images"); int imgsCount = imgs.Count(); for (int i = 0; i < 100; i++) { BooksCollection.Add(new Book() { Id = i + 1, Name = $"Name_{i + 1}", ISBN = $"ISBN_{Guid.NewGuid().ToString("N")}", Topic = $"Topic_{i + 1}", ImgSource = GetImgSourceViaUrl(imgs[i % imgsCount]) }); } } private ImageSource GetImgSourceViaUrl(string imgUrl) { BitmapImage bmi = new BitmapImage(); bmi.BeginInit(); bmi.UriSource = new Uri(imgUrl, UriKind.RelativeOrAbsolute); bmi.EndInit(); if (bmi.CanFreeze) { bmi.Freeze(); } return bmi; } private ObservableCollection<Book> booksCollection; public ObservableCollection<Book> BooksCollection { get { return booksCollection; } set { if (value != booksCollection) { booksCollection = value; OnPropertyChanged(nameof(BooksCollection)); } } } public event PropertyChangedEventHandler? PropertyChanged; private void OnPropertyChanged(string propertyName) { var handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } } } // using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; namespace WpfApp155 { public class DelCommand : ICommand { private Action<object?> execute; private Predicate<object?> canExecute; public DelCommand(Action<object?> executeValue, Predicate<object?> canExecuteValue = null) { execute = executeValue; canExecute = canExecuteValue; } public event EventHandler? CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public bool CanExecute(object? parameter) { if (canExecute == null) { return true; } return canExecute(parameter); } public void Execute(object? parameter) { execute(parameter); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Media; namespace WpfApp155 { public class Book { public int Id { get; set; } public string Name { get; set; } public string ISBN { get; set; } public string Topic { get; set; } public ImageSource ImgSource { get; set; } } }
<Window x:Class="WpfApp155.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:local="clr-namespace:WpfApp155" WindowState="Maximized" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <local:UCLbx/> </Grid> </Window>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现