xml 【执笔中】【WPF】【MVVM】动的なコントロールに対して,动的なコンテキストメニューを构成する。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml 【执笔中】【WPF】【MVVM】动的なコントロールに対して,动的なコンテキストメニューを构成する。相关的知识,希望对你有一定的参考价值。
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.Input;
using System.Windows.Media;
namespace ContextMenuTest.ViewModel
{
public class MyViewModel : INotifyPropertyChanged
{
private ObservableCollection<BorderItem> borders;
public ObservableCollection<BorderItem> Borders
{
get { return this.borders; }
set
{
if (this.borders != value) {
this.borders = value;
NotifyPropertyChaned("Borders");
}
}
}
#region INotifyPropertyChanged メンバー
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChaned(string arg)
{
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(arg));
}
}
#endregion
public MyViewModel()
{
var menu1 = new[]
{
new ContextMenuItem("Add", null, Visibility.Visible),
new ContextMenuItem("Edit", null, Visibility.Visible),
new ContextMenuItem("Copy", null, Visibility.Collapsed),
new ContextMenuItem("Delete", null, Visibility.Visible),
};
var menu2 = new[]
{
new ContextMenuItem("Add", null, Visibility.Visible),
};
var menu3 = new[]
{
new ContextMenuItem("Copy", null, Visibility.Collapsed),
new ContextMenuItem("Delete", null, Visibility.Visible),
};
this.Borders = new ObservableCollection<BorderItem>() {
new BorderItem(new SolidColorBrush(Colors.Red), menu1),
new BorderItem(new SolidColorBrush(Colors.Blue), menu2),
new BorderItem(new SolidColorBrush(Colors.Purple), menu3),
};
}
}
public class BorderItem : INotifyPropertyChanged
{
private Brush backgroundColor;
public Brush BackgroundColor
{
get { return this.backgroundColor; }
set
{
if (this.backgroundColor != value) {
this.backgroundColor = value;
NotifyPropertyChaned("BackgroundColor");
}
}
}
private ObservableCollection<ContextMenuItem> menus;
public ObservableCollection<ContextMenuItem> Menus
{
get { return this.menus; }
set
{
if (this.menus != value) {
this.menus = value;
NotifyPropertyChaned("Menus");
}
}
}
#region INotifyPropertyChanged メンバー
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChaned(string arg)
{
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(arg));
}
}
#endregion
public BorderItem(Brush b, ContextMenuItem[] m)
{
this.BackgroundColor = b;
this.Menus = new ObservableCollection<ContextMenuItem>(m);
}
}
public class ContextMenuItem : INotifyPropertyChanged
{
private string title;
public string Title
{
get { return this.title; }
set
{
if (this.title != value) {
this.title = value;
NotifyPropertyChaned("Title");
}
}
}
private ICommand command;
public ICommand Command
{
get { return this.command; }
set
{
if (this.command != value) {
this.command = value;
NotifyPropertyChaned("Title");
}
}
}
private Visibility visibility;
public Visibility Visibility
{
get { return this.visibility; }
set
{
if (this.visibility != value) {
this.visibility = value;
NotifyPropertyChaned("Visibility");
}
}
}
#region INotifyPropertyChanged メンバー
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChaned(string arg)
{
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(arg));
}
}
#endregion
public ContextMenuItem(string t, ICommand c, Visibility v)
{
this.Title = t;
this.Command = c;
this.Visibility = v;
}
}
}
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;
using ContextMenuTest.ViewModel;
namespace ContextMenuTest
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var vm = new MyViewModel();
this.DataContext = vm;
}
}
}
<Window x:Class="ContextMenuTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Border Width="100" Height="100" Background="Aqua">
<Border.ContextMenu>
<ContextMenu>
<MenuItem Header="Add"/>
<MenuItem Header="Edit"/>
<MenuItem Header="Delete"/>
</ContextMenu>
</Border.ContextMenu>
</Border>
<ItemsControl Grid.Column="1" ItemsSource="{Binding Borders}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Width="30" Height="30" Background="{Binding BackgroundColor}">
<Border.ContextMenu>
<ContextMenu ItemsSource="{Binding Menus}">
<ContextMenu.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Header" Value="{Binding Title}" />
<Setter Property="Command" Value="{Binding Command}" />
<Setter Property="Visibility" Value="{Binding Visibility}"/>
</Style>
</ContextMenu.ItemContainerStyle>
</ContextMenu>
</Border.ContextMenu>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Window>
以上是关于xml 【执笔中】【WPF】【MVVM】动的なコントロールに対して,动的なコンテキストメニューを构成する。的主要内容,如果未能解决你的问题,请参考以下文章
xml 【WPF】コントロールのフォーカスのと风格FocusVisualStyle
xml 【WPF】亲要素に含まれる子要素だけコンテキストメニューを出さないようにするには
INotifyPropertyChange(WPF,MVVM)的问题