wpf自定义控件(包含依赖属性以及事件)

wpf自定义控件(包含依赖属性以及事件)

2023年7月19日发(作者:)

wpf⾃定义控件(包含依赖属性以及事件)创建了这个依赖属性,就可以直接在对应的控件中使⽤了,就像是button中⼀开始就内置的width等属性⼀样,这个在设计⾃定义控件的时候⽤的尤其多下⾯讲的是⾃定义分页控件代码:

xmlns:d="/expression/blend/2008"

xmlns:local="clr-namespace:WPFDataGridPaging" mc:Ignorable="d"

d:DesignHeight="30" d:DesignWidth="220">

界⾯:

后台代码: public partial class Pager : UserControl { #region 声明事件和依赖属性

public static RoutedEvent FirstPageEvent; public static RoutedEvent PreviousPageEvent; public static RoutedEvent NextPageEvent; public static RoutedEvent LastPageEvent; public static readonly DependencyProperty CurrentPageProperty; public static readonly DependencyProperty TotalPageProperty; #endregion public string CurrentPage { get { return (string)GetValue(CurrentPageProperty); } set { SetValue(CurrentPageProperty, value); } } public string TotalPage { get { return (string)GetValue(TotalPageProperty); } set { SetValue(TotalPageProperty, value); } } public Pager() { InitializeComponent(); } static Pager() { #region 注册事件以及依赖属性 //注册FirstPageEvent事件,事件的拥有者是Pager,路由事件的名称是FirstPage,这是唯⼀的 FirstPageEvent = erRoutedEvent("FirstPage", ,

typeof(RoutedEventHandler), typeof(Pager)); PreviousPageEvent = erRoutedEvent("PreviousPage", ,

typeof(RoutedEventHandler), typeof(Pager)); NextPageEvent = erRoutedEvent("NextPage", , typeof(RoutedEventHandler), typeof(Pager)); LastPageEvent = erRoutedEvent("LastPage", ,

typeof(RoutedEventHandler), typeof(Pager)); //注册⾃定义的依赖属性 CurrentPageProperty = er("CurrentPage", typeof(string), typeof(Pager), new PropertyMetadata(, new PropertyChangedCallback(OnCurrentPageChanged))); TotalPageProperty = er("TotalPage",

typeof(string), typeof(Pager), new PropertyMetadata(, new PropertyChangedCallback(OnTotalPageChanged))); #endregion } public event RoutedEventHandler FirstPage { add { AddHandler(FirstPageEvent, value); } remove { RemoveHandler(FirstPageEvent, value); } } public event RoutedEventHandler PreviousPage { add { AddHandler(PreviousPageEvent, value); } remove { RemoveHandler(PreviousPageEvent, value); } } public event RoutedEventHandler NextPage { add { AddHandler(NextPageEvent, value); } remove { RemoveHandler(NextPageEvent, value); } } public event RoutedEventHandler LastPage { add { AddHandler(LastPageEvent, value); } remove { RemoveHandler(LastPageEvent, value); } } ///

/// 依赖属性回调⽅法 /// /// /// public static void OnTotalPageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Pager p = d as Pager; if (p != null) { Run rTotal = (Run)me("rTotal"); = (string)ue; } } private static void OnCurrentPageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Pager p = d as Pager; if (p != null) { Run rCurrrent = (Run)me("rCurrent"); = (string)ue; } } private void FirstPageButton_Click(object sender, RoutedEventArgs e) { RaiseEvent(new RoutedEventArgs(FirstPageEvent, this)); } private void PreviousPageButton_Click(object sender, RoutedEventArgs e) { RaiseEvent(new RoutedEventArgs(PreviousPageEvent, this)); } private void NextPageButton_Click(object sender, RoutedEventArgs e) { RaiseEvent(new RoutedEventArgs(NextPageEvent, this)); } private void LastPageButton_Click(object sender, RoutedEventArgs e) { RaiseEvent(new RoutedEventArgs(LastPageEvent, this)); } }

上⾯就是⾃定义的⼀个分页控件,如果我们想要使⽤这个控件,如下:

HorizontalAlignment="Center" ="1">

界⾯:

后台代码: ///

/// Interaction logic for /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); //这⾥⼀定要注意,⼀定要将模型加到上下⽂中,要不然依赖属性和事件都不会触发,在实际开发中是不可能⼀打开 //界⾯就开始加载出来数据的,所以可以通过ObservableCollection形式绑定表格数据, //⽽需要绑定事件和依赖属性的需要将其绑定到上下⽂中 DataContext = new MainViewModel(); } }

MainViewModel: public class MainViewModel : ViewModel { private ICommand _firstPageCommand; public ICommand FirstPageCommand { get { return _firstPageCommand; } set { _firstPageCommand = value; } } private ICommand _previousPageCommand; public ICommand PreviousPageCommand { get { return _previousPageCommand; } set { _previousPageCommand = value; } } private ICommand _nextPageCommand; public ICommand NextPageCommand { get { return _nextPageCommand; } set { _nextPageCommand = value; } } private ICommand _lastPageCommand; public ICommand LastPageCommand { get { return _lastPageCommand; } set { _lastPageCommand = value; } } private int _pageSize; public int PageSize { get { return _pageSize; } set { if(_pageSize != value) { _pageSize = value; OnPropertyChanged("PageSize"); } } } private int _currentPage; public int CurrentPage { get { return _currentPage; } set { if(_currentPage != value) { _currentPage = value; OnPropertyChanged("CurrentPage"); } } } private int _totalPage; public int TotalPage { get { return _totalPage; } set { if(_totalPage != value) { _totalPage = value; OnPropertyChanged("TotalPage"); } } } ///

/// ObservableCollection表⽰⼀个动态数据集合,它可在添加、删除项⽬或刷新整个列表时提供通知。 /// 在xaml中绑定FakeSource之后,运⾏中FakeSource这⾥的数据改变的话 /// private ObservableCollection _fakeSoruce; public ObservableCollection FakeSource { get { return _fakeSoruce; } set { if(_fakeSoruce != value) { _fakeSoruce = value; OnPropertyChanged("FakeSource"); } } } private List _source; public MainViewModel() { _currentPage = 1; _pageSize = 20; FakeDatabase fake = new FakeDatabase(); _source = teFakeSource(); _totalPage = _ / _pageSize; _fakeSoruce = new ObservableCollection(); List result = _(20).ToList(); _(); _ge(result); _firstPageCommand = new DelegateCommand(FirstPageAction); _previousPageCommand = new DelegateCommand(PreviousPageAction); _nextPageCommand = new DelegateCommand(NextPageAction); _lastPageCommand = new DelegateCommand(LastPageAction); } private void FirstPageAction() { CurrentPage = 1; var result = _(_pageSize).ToList(); _(); _ge(result); } private void PreviousPageAction() { if(CurrentPage == 1) { return; } List result = new List(); if(CurrentPage == 2) { result = _(_pageSize).ToList(); } else { result = _((CurrentPage - 2) * _pageSize).Take(_pageSize).ToList(); } _(); _ge(result); CurrentPage--; } private void NextPageAction() { if(CurrentPage == _totalPage) { return; } List result = new List(); result = _(CurrentPage * _pageSize).Take(_pageSize).ToList(); _(); _ge(result); CurrentPage++; } private void LastPageAction() { CurrentPage = TotalPage; int skipCount = (_totalPage - 1) * _pageSize; int takeCount = _ - skipCount; var result = _(skipCount).Take(takeCount).ToList(); _(); _ge(result); } }

其它补充代码:

DelegateCommandpublic class DelegateCommand : ICommand { private static bool CanExecute(T parameter) { return true; } private readonly Action _execute; private Func _canExecute; public DelegateCommand(Action execute, Func canExecute = null) { if (execute == null) { throw new ArgumentNullException("execute is null."); } _execute = execute; _canExecute = canExecute ?? CanExecute; } public bool CanExecute(object parameter) { return _canExecute(TranslateParameter(parameter)); } public event EventHandler CanExecuteChanged { add { if (_canExecute != null) { ySuggested += value; } } remove { if (_canExecute != null) { ySuggested -= value; } } } public void Execute(object parameter) { _execute(TranslateParameter(parameter)); } private T TranslateParameter(object parameter) { T value = default(T); if (parameter != null && typeof(T).IsEnum) value = (T)(typeof(T), (string)parameter); else value = (T)parameter; return value; } } public class DelegateCommand : DelegateCommand { public DelegateCommand(Action execute,Func canExecute = null) : base(obj => execute(), (canExecute == null ? null : new Func(obj => canExecute()))) { } }

FakeDatabase public class FakeDatabase { public int Id { get; set; } public string ItemName { get; set; } public List GenerateFakeSource() { List source = new List();

for (int i = 0; i < 1000; i++) { FakeDatabase item = new FakeDatabase() { Id = i, ItemName = "Item" + ng() }; (item); } return source; } public FakeDatabase() {

} }

Extension public static class Extension { public static void AddRange(this ObservableCollection collection, IEnumerable items) { ().ForEach(); } }

ViewModel public class ViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }

ViewModel

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1689721201a280981.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信