using Prism.Commands; using Prism.Mvvm; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Controls; namespace PDF_Office.ViewModels.HomePanel.PDFTools { public class PDFToolsContentViewModel : BindableBase { #region 属性 /// /// 扩展/收缩 /// private bool _isExpendTools = false; public bool IsExpendTools { get { return _isExpendTools; } set { SetProperty(ref _isExpendTools, value); } } private bool _isDropTools = false; public bool IsDropTools { get { return _isDropTools; } set { SetProperty(ref _isDropTools, value); } } #endregion #region Command and Event public DelegateCommand OpenMenuCommand { get; set; } public DelegateCommand ExpendCommand { get; set; } public DelegateCommand CustomCommand { get; set; } public event EventHandler ExpendToolsHanlder; #endregion public PDFToolsContentViewModel() { InitCommand(); } private void InitCommand() { OpenMenuCommand = new DelegateCommand(OpenMenu); ExpendCommand = new DelegateCommand(Expend); CustomCommand = new DelegateCommand(Custom); } private void Custom(object obj) { IsDropTools = true; } private void Expend() { IsExpendTools = !IsExpendTools; ExpendToolsHanlder?.Invoke(null, IsExpendTools); } private void OpenMenu(object obj) { var menu = App.Current.FindResource("ExpendToolsContextMenu") as ContextMenu; var btn = obj as Button; if (menu != null && btn != null) { if (menu.Items.Count == 1) { var item = menu.Items[0] as MenuItem; if (_isExpendTools) { item.Header = "收缩"; } else { item.Header = "展开"; } item.Command = ExpendCommand; //btn.ContextMenu = menu; menu.PlacementTarget = btn; menu.IsOpen = true; } } } } }