WPF简单使用-主要接口

一、INotifyPropertyChanged(View-ViewModel属性值改变通知属性)

1、封装INotifyPropertyChanged接口

using System.ComponentModel;
封装INotifyPropertyChanged接口
 public class NotifyBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void DoNotify([CallerMemberName] string propName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
        }
    }

2、使用NotifyBase.cs(存放于Common路径下)

A、引用Common命名空间

B、使用方法

例:

    public class WindElementViewData :NotifyBase
    {//快捷键 propfull
        private string _viewTime = "未查看";
        public string ViewTime { get { return _viewTime; } set { _viewTime = value; DoNotify("ViewTime"); } }
}

二、ICommand(View Command绑定接口 在ViewModel中进行具体实现)

 1、封装ICommand接口

using System.Windows.Input;
标封装ICommand接口题
 public class CommandBase : ICommand
    {
        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return DoCanExcute?.Invoke(parameter) == true ? true : false;
        }
        public void Execute(object parameter)
        {
            DoExcute?.Invoke(parameter);
        }
        public Action<object> DoExcute { get; set; }
        public Func<object, bool> DoCanExcute { get; set; }
    }

2、使用CommandBase .cs(存放于Common路径下)

A、引用Common命名空间

B、使用方法

例:

  public TestDemo()
        {
            TestCommand = new CommandBase()
            {
                DoCanExcute = new Func<object, bool>(o => { return true; }),
                DoExcute = new Action<object>(DoTestCommand)
            };

        }

三、IValueConverter(属性值转换)

1、cs中使用方法

using System.Windows.Data
 public class BackgroundConverter : IValueConverter
    {
        public  object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            ListViewItem item = (ListViewItem)value;
            ListView listView = ItemsControl.ItemsControlFromItemContainer(item) as ListView;
            // Get the index of a ListViewItem
            int index = listView.ItemContainerGenerator.IndexFromContainer(item);

            if (index % 2 == 0)
            {
                return Brushes.LightSteelBlue;
            }
            else
            {
                return Brushes.White;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

2、xmal中使用

A、引用指定的命名控件 xmlns:ct="命名空间"
B、在需要使用转换的控件属性中使用Converter= ct:BackgroundConverter 
  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值