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); } } ///
上⾯就是⾃定义的⼀个分页控件,如果我们想要使⽤这个控件,如下: HorizontalAlignment="Center" ="1">
界⾯:
后台代码: ///
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"); } } } ///
其它补充代码:
DelegateCommandpublic class DelegateCommand
FakeDatabase public class FakeDatabase { public int Id { get; set; } public string ItemName { get; set; } public 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
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条)