原创: 本示例以一个共享控件为例,对样式资源加以说明,方便对样式有个更深层次的了解,主要有以下特点: 1.使用简单MVVM模式,使用命令代替UI事件。 2.使用到各种样式。 3.将控件共享化。 4.使用命令触发器。 public MainWindow() { InitializeComponent(); Action<DataGrid, DelegateCommand<IList<object>>, string> bindData = (xamDataGrid, selectedRowsCommand, rowsPropertyName) => { BindingOperations.SetBinding(xamDataGrid, DataGrid.ItemsSourceProperty, new Binding { Source = ViewModel, Path = new PropertyPath(rowsPropertyName), Mode = BindingMode.OneWay }); // TODO : 全选命令未绑定到vm var contextMenu = (MenuItem)xamDataGrid.ContextMenu.Items[0]; contextMenu.Command = ApplicationCommands.SelectAll; contextMenu.Header = "全选"; var commandTrigger = (EventCommandTrigger)CommandSource.GetTriggers(xamDataGrid); BindingOperations.SetBinding(commandTrigger, EventCommandTrigger.CommandParameterProperty, new Binding { Source = xamDataGrid.SelectedItems, }); commandTrigger.Command = selectedRowsCommand; commandTrigger.RoutedEvent = DataGrid.SelectionChangedEvent; commandTrigger.UpdateCommandParameter = true; }; const string SHARED_DATAGRID = "xgShared"; var viewModel = (MainViewModel)ViewModel; var xdgSystem = (DataGrid)Resources[SHARED_DATAGRID]; bindData(xdgSystem, viewModel.SelectedSystemRowsCommand, "SystemRows"); placeSystem.Content = xdgSystem; var xdgImport = (DataGrid)Resources[SHARED_DATAGRID]; bindData(xdgImport, viewModel.SelectedImportRowsCommand, "ImportRows"); placeImport.Content = xdgImport; }
- 1
- 粉丝: 1
- 资源: 7
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助