\

In the cone of light, all is fate

【WPF】XAML 常用

鼠标拖动列表类的操作,直接将View的X与VM中的属性双向绑定会非常卡,可以用Behaviors中转

//交互和计算在这个类中进行,将值传给VM
 class MouseActionBehavior : Behavior<UIElement>

 

以下是一个ListBox的ItemSource绑定了一个“List<int>”,Command和CommandParameter分别绑定了VM中不同层级的对象

image

Style的基本用法

image

写在资源字典上的Style也可以绑定数据,DataTrigger用于非控件自身的属性

image

资源字典中可以引入另一个资源字典

image

转换器、样式在XAML页面引用

image

多重绑定,绑定当前XAML自身的属性

image

绑定当前XAML其他元素的属性

Height="{Binding ElementName=XX,Path=ActualHeight}

Grid.Column或者Row绑定VM



简单的Command

class MyCommand : ICommand
    {
        private readonly Action<object> _execAction;
        private readonly Func<object, bool> _changeFunc;

        public MyCommand(Action<object> execAction, Func<object, bool> changeFunc)
        {
            _execAction = execAction;
            _changeFunc = changeFunc;
        }

        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return this._changeFunc.Invoke(parameter);
        }

        public void Execute(object parameter)
        {
            _execAction.Invoke(parameter);
        }
    }
//使用
public ICommand XXXCommand => new MyCommand(XXXAction, CanExc => true);

private void XXXAction(object obj)
        {
            //...
        }

简单的VMBase

class MyViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
posted @ 2024-01-17 15:40  Ymrt  阅读(21)  评论(0编辑  收藏  举报