c#面向对象问题 WPF简单数据驱动

WPF

开发工具:Visual Studio 2019 16.11 Community

基础框架:.NET5 Framework4.8

UI框架:WPF

事件驱动 数据驱动

行为绑定:

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

namespace WpfApp2
{
    // 实现接口
    class CommandBase : ICommand
    {
        public event EventHandler CanExecuteChanged;


        public bool CanExecute(object parameter)
        {
            // 绑定了命令的控件是否可用
            //return true;
            return DoCanExecute?.Invoke(parameter) == true;//false
        }

        // parameter=123 CommandParameter="123"
        public void Execute(object parameter)
        {
            // 控制逻辑
            DoAction?.Invoke(parameter);
        }

        // 委托
        public Action<object> DoAction { get; set; }

        public Func<object, bool> DoCanExecute { get; set; }

        public void RaiseCanChanged()
        {
            CanExecuteChanged?.Invoke(this,new EventArgs());
        }
    }
}

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 WpfApp2
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        res mainwindow = null;
        
        public MainWindow()
        {
            InitializeComponent();
            mainwindow = new res();
            this.DataContext = mainwindow;
        }

        
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            mainwindow.Value = "100";
        }
    }
}

Command="{Binding ValueCommand}"
CommandParameter="123"
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media;

namespace WpfApp2
{
    public class res:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public string _Value = "hello word";
        private Brush _valueColor = Brushes.Orange;

        public string Value
        {
            get { return _Value; }
            set
            {
                _Value = value;
                PropertyChanged?.Invoke(this,new PropertyChangedEventArgs("Value"));
                if (value == "100")
                {
                    valueColor = Brushes.Green;
                    
                }
                
            }
        }

        public Brush valueColor
        {
            get { return _valueColor; }
            set { _valueColor = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("valueColor"));
            }
        }


        private ICommand _valueCommand;

        // 点击就触发这个接口
        public ICommand ValueCommand
        {
            get
            {
                if (_valueCommand == null)
                {
                    _valueCommand = new CommandBase()
                    {
                        DoAction = new Action<object>(ValueCommandAction),
                        DoCanExecute = new Func<object, bool>(CanExecute)
                    };
                    
                }
                return _valueCommand;
            }
            set { _valueCommand = value; }
        }

        private void ValueCommandAction(object obj)
        {
            Value = "100";
        }

        private bool CanExecute(object obj)
        {
            return !string.IsNullOrEmpty(Value);
        }


    
    }
}

// pass 练习复习

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

namespace 方法汇总
{

    // ---------------------c#方法汇总----------------------------------
    /*
        静态方法
                    特点1.生命周期:一旦创建,直到应用结束,才会结束
                        2.全局都能访问
                        3.效率高(数据都在内存中)
                    用处:
                        用户登录信息,系统配置...
                    注意:静态的东西创建多了 占用内存大(不是必要的情况下不要创建静态类)
                    调用:不能实例化
                    code:
                        public static string test()=> "hello word"; 

        构造方法
                    用处:初始化对象或初始化一些列数据 方法与类同名
                    特点:默认有一个无参数默认方法,可以多个并重载

        析构方法
                    作用:释放对象 GC垃圾回收机制在调用
                    ~开头 有 Dispose方法都是非托管资源! Close() 关闭资源,没有完全释放. Dispose() 完全释放
                    99%情况下不需要自己写
        虚方法--virtual
                    作用:允许子类/派生类,进行重写 实现不同的功能
                    特点:
                    base. 调用的是基类的方法.
        重写方法--override
                    重写父类虚方法


        抽象方法--abstract
                    抽象方法需要写在抽象类中,规范好让子类去实现!不能new
                    使用场合:强制性一定要实现
                    与接口区别与使用场合:抽象单继承,接口多继承 接口命名开头是I 2抽象可以写各种方法虚方法,接口就不行
                        抽象类一般用于不会经常改动,然后抽象范围大一点的
                        具体动作可以使用接口

                    

        扩展方法--ExtendMethod
                    定义: 在静态类下定义静态方法,传参 this 类型 变量
                    场合: 调用密封类中的对象,属性,方法.(扩展密封类)
                          扩展接口. 3-在Linq链式编程
                
    */




    

    // 定义一个静态类
    public static class PersonExten
    {
        public static void ShowPhone(this Person person)
        {
            Console.WriteLine(person.GetPhone());
        }
    }

    // 扩展方法// 假如类不是自己写的
    public sealed class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Phone { get; set; }

        public string GetPhone()
        {
            return Phone;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Person person = new Person() { Name = "lddragon", Age = 18, Phone = "1660287104*" };
            PersonExten.ShowPhone(person);
            person.ShowPhone();

            TestI res = new TestI();
            res.Maltiply(1,2);

            Console.ReadKey();
        }
    }

    public interface ICalculate
    {
        int Add(int a, int b);
    }

    public static class InterfaceExtend
    {
        public static int Sub(this ICalculate ic,int a, int b)
        {
            return a - b;
        }
        public static int Maltiply(this ICalculate ic,int a, int b)
        {
            return a * b;
        }
        public static int Division(this ICalculate ic,int a, int b)
        {
            return a / b;
        }
       
    }

    class TestI : ICalculate
    {
        public int Add(int a, int b)
        {
            throw new NotImplementedException();
        }
    }



}

吾虽浪迹,却未迷失本心
原文地址:https://www.cnblogs.com/lddragon1/p/15527248.html