WPF 自定义命令实现

1.自定义控件 MiniView

xaml:

<UserControl x:Class="OwnCommandDemo.MiniView"
             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:OwnCommandDemo"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Border CornerRadius="5" BorderBrush="LawnGreen" BorderThickness="2">
        <StackPanel>
            <TextBox x:Name="textBox1" Margin="5"/>
            <TextBox x:Name="textBox2" Margin="5,0"/>
            <TextBox x:Name="textBox3" Margin="5"/>
            <TextBox x:Name="textBox4" Margin="5,0"/>
        </StackPanel>
    </Border>    
</UserControl>
 

cs:

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;

namespace OwnCommandDemo
{
    /// <summary>
    /// MiniView.xaml 的交互逻辑
    /// </summary>
    public partial class MiniView : UserControl,IView
    {
        public MiniView()
        {
            InitializeComponent();
        }

        public bool IsChanged { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

        public void Clear()
        {
            this.textBox1.Clear();
            this.textBox2.Clear();
            this.textBox3.Clear();
            this.textBox4.Clear();
        }

        public void Refresh()
        {
            throw new NotImplementedException();
        }

        public void Save()
        {
            throw new NotImplementedException();
        }

        public void SetBinding()
        {
            throw new NotImplementedException();
        }
    }
}
 

2.IView接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OwnCommandDemo
{
    public interface IView
    {
        // 属性
        bool IsChanged { get; set; }
        // 方法 
        void SetBinding();
        void Refresh();
        void Clear();
        void Save();
    }
}
 

3.主界面

xaml:

<Window x:Class="OwnCommandDemo.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:OwnCommandDemo"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <local:MyCommandSource x:Name="ctrlClear" Margin="10">
            <TextBlock Text="清除" FontSize="16" TextAlignment="Center" Background="LightBlue" Width="80"/>
        </local:MyCommandSource>
        <local:MiniView x:Name="miniView"/>
    </StackPanel>
</Window>
 

cs:

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;

namespace OwnCommandDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            // 声明命令并使用命令源和目标与之关联
            ClearCommand clearCmd = new ClearCommand();
            this.ctrlClear.Command = clearCmd;
            this.ctrlClear.CommandTarget = this.miniView;
        }
    }

    public class ClearCommand : ICommand
    {
        // 当命令可执行状态发生改变时,应当被激发
        public event EventHandler CanExecuteChanged;
        // 用于判断命令是否可以执行
        public bool CanExecute(object parameter)
        {
            throw new NotImplementedException();
        }
        // 命令执行,带有与业务相关的Clear逻辑
        public void Execute(object parameter)
        {
            IView view = parameter as IView;
            if (view != null)
            {
                view.Clear();
            }
        }
    }

    // 自定义命令源
    public class MyCommandSource : UserControl, ICommandSource
    {
        // 继承自ICommandSource的三个属性
        public ICommand Command { get; set; }

        public object CommandParameter { get; set; }

        public IInputElement CommandTarget { get; set; }
        // 在组件被单击时连带执行命令
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            // 在命令目标上执行命令,或称为让命令作用于命令目标
            if (this.CommandTarget != null)
            {
                this.Command.Execute(this.CommandTarget);
            }
        }
    }
}
 

实现效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
WPF(Windows Presentation Foundation)是微软的一种用户界面技术,可用于创建各种Windows应用程序。TreeViewWPF中的一种控件,用于显示层次结构的数据。 当我们需要自定义TreeView时,可以使用WPF提供的各种功能和特性来实现。下面是一些实现自定义TreeView的方法: 1. 自定义树节点的外观:可以使用WPF的样式和模板来修改树节点的外观,例如更改节点的背景颜色、字体样式等。可以使用TreeView控件的ItemTemplate属性来为每个节点定义一个数据模板。 2. 添加功能按钮:可以为TreeView控件添加自己需要的功能按钮,比如展开/折叠按钮、添加/删除节点按钮等。可以使用WPF中的Button控件实现这些按钮,并与TreeView的事件关联。 3. 添加交互行为:可以通过使用WPF提供的命令功能,为TreeView控件添加交互行为。例如,可以为每个节点添加一个命令,当用户双击节点时执行该命令。 4. 实现节点的拖拽和放置:可以通过使用WPF的拖放功能,实现节点的拖拽和放置。可以为每个节点添加一个DragStarted事件,当用户拖动节点时触发该事件,并将节点的数据作为数据对象传递给拖动操作。同时,可以使用TreeViewDropTarget控件作为拖放目标,通过处理Drop事件实现节点的放置。 总的来说,WPF提供了丰富的功能和特性,使我们能够很容易地自定义TreeView控件。通过使用WPF的样式、模板、命令和拖放功能,我们可以实现一个完全符合我们需求的TreeView控件,并为用户提供更好的用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小蚂蚁_CrkRes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值