linux怎么做网站,四川建设厅电话网站,做网站zwnet,建设外包网站WPF项目搭建 版权声明#xff1a;本文为博主初学经验#xff0c;未经博主允许不得转载。 一、前言 记录在学习与制作WPF过程中遇到的解决方案。 使用MVVM的优点是 数据和视图分离#xff0c;双向绑定#xff0c;低耦合#xff0c;可重用行#xff0c;相对独立的设计和逻辑…WPF项目搭建 版权声明本文为博主初学经验未经博主允许不得转载。 一、前言 记录在学习与制作WPF过程中遇到的解决方案。 使用MVVM的优点是 数据和视图分离双向绑定低耦合可重用行相对独立的设计和逻辑 二、配置 系统环境win10 开发工具Visual Studio 2017 开发语言C#.WPF (MVVM框架) 数据库SQLiteStudio 三、附件 vs_enterprise.exe 在线安装 Visual Studio 2017 开发工具SQLiteStudio.zip 免安装Sqlite轻量数据库操作工具WPF-MVVM-Work.zip 项目源代码四、步骤 1. 创建项目 2. 文件夹层级 建文件夹Model, ViewModel, View, Resources, Lib, Service, Common Model是模型层属性类存放的地方也就是存放不涉及业务逻辑的代码 例如列表元素接口参数枚举数据交互的参数模型和View基础元素属性等等 ViewModel是视图模型层是MVVM实现业务逻辑代码的地方 例如操作视图界面数据的呈现界面按钮触发的事件和操作数据库前后对界面交互的事件 View是视图层是窗体布局实现的地方也就是呈现给用户交互使用的界面窗体 例如登录页面查询页面新增和编辑的页面等 Resources是资源库层里面存放声音图片和样式布局等统一调用外部资源的地方 Lib是引用层放置一些第三方引用便于调用也可以用nuget统一管理第三方dll Service是服务层是实现对数据库或者对站点接口的操作进行数据持久化和数据交互 Common是工具层是存放一些公共的代码统一调用简洁业务逻辑代码的冗余 注可以建文件夹但更推荐新建类库放置以上层级 3. 控件的使用与布局 3.1 从工具箱中直接拖拉控件 3.2 直接在代码中编辑控件代码 4. 代码关联 4.1 View与ViewModel的交互关联 - View后台的代码关联 public MainWindow() //类名{InitializeComponent(); //后台代码有这句就实现了View和ViewModel的绑定DataContext new MainWindowViewModel(); } - 文本的绑定 前端TextBox Text{Binding TxtInput}/ 后端 public string TxtInput { get _txtInput; set { _txtInput value; //用RaisePropertyChanged刷新前端绑定控件的输入框文本内容 RaisePropertyChanged(TxtInput); } } private string _txtInput; - 事件的绑定 前端Button Content添加 Command{Binding BtnAddContent}/ 后端 public MainWindowViewModel() { //按钮事件与业务的衔接 也可以在BtnAddContent的Set中写 BtnAddContent new RelayCommand(AddContent); } //前端绑定的 添加按钮 Command事件 public RelayCommand BtnAddContent { get; set; } private void AddContent() { //按钮事件处理的业务逻辑代码 } - 样式的绑定 TextBox Style{StaticResource TxbTrigger} Tag序号... /Button Content添加 Template{StaticResource DefaultButton} / ”TxbTrigger是输入框水印样式资源DefaultButton是按钮样式资源模板详细样式代码查阅源码中的Resources-Style;“ 4.2 数据绑定和命令绑定的代码如果用第三方框架引用就不需要编写以下两个方法类 //数据绑定 public class ViewModelBase : INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;protected void RaisePropertyChangedT(ExpressionFuncT action){var propertyName GetPropertyName(action);RaisePropertyChanged(propertyName);}private static string GetPropertyNameT(ExpressionFuncT action){var expression (MemberExpression)action.Body;var propertyName expression.Member.Name;return propertyName;}public void RaisePropertyChanged(string propertyName){if (PropertyChanged ! null)PropertyChanged(this,new PropertyChangedEventArgs(propertyName));}} //命令绑定 public class RelayCommand : ICommand{public Action ExecuteAction; //执行方法public Actionobject ExecuteCommand; //执行方法 带参数public Funcobject, bool CanExecuteCommand; //执行方法的条件public RelayCommand(Action action)// 执行事件{ExecuteAction action;} //执行带参数的事件public RelayCommand(Actionobject action){ExecuteCommand action;} //根据条件执行带参数的事件public RelayCommand(Actionobject action, Funcobject, bool can){ExecuteCommand action;CanExecuteCommand can;} //当命令可执行状态发生改变时应被激活public event EventHandler CanExecuteChanged; //用于判断命令是否可以执行 public bool CanExecute(object parameter){if (ExecuteAction ! null) return true;return CanExecuteCommand null || CanExecuteCommand(parameter);} //命令执行public void Execute(object parameter){if (ExecuteCommand ! null) ExecuteCommand(parameter);else ExecuteAction();}} ViewModel的业务类需要继承ViewModelBase public class MainWindowViewModel : ViewModelBase 前端的DataGrid绑定的数据需要用ObservableCollection类型定义列表 public ObservableCollectionAddModel AddContent{get _addContent;set{_addContent value;RaisePropertyChanged(AddContent); }}private ObservableCollectionAddModel _addContent new ObservableCollectionAddModel(); AddModel是Model中的属性类代表DataGrid中绑定的列名指向 DataGrid前端代码: DataGrid x:NameDgTimes ItemsSource{Binding AddContent} AutoGenerateColumnsFalseSelectedItem{Binding SelectTime,UpdateSourceTriggerPropertyChanged}DataGrid.InputBindings !--双击事件--MouseBinding GestureLeftDoubleClick Command{Binding DgDoubleClick} CommandParameter{Binding ElementNameDgTimes,PathSelectedItem}/!--单击事件也可以在SelectedItem选中事件中的set属性编辑业务代码-- MouseBinding GestureLeftClick Command{Binding DgClick}CommandParameter{Binding ElementNameDgTimes,PathSelectedItem}//DataGrid.InputBindingsDataGrid.ColumnsDataGridTextColumn Header序号 Binding{Binding RowIndex}/DataGridTemplateColumn Header勾选 MinWidth30DataGridTemplateColumn.CellTemplateDataTemplateGrid !--图片的绑定注意不能为null或者空值不然会加载超慢--Image Width20 Height20 Source{Binding Photo}//Grid/DataTemplate/DataGridTemplateColumn.CellTemplate/DataGridTemplateColumnDataGridTextColumn Header输入内容 Binding{Binding AddContent}/DataGridTextColumn Header时间 Binding{Binding AddTime}//DataGrid.Columns
/DataGrid 注由于整个制作项目的操作视频文件过大如有所需留邮箱地址给博主 5. 执行效果 五、注意事项 本次项目制作并没有使用数据库的操作源代码中也是没有 源码中包括了对输入框限制数字的输入方法 源码中包括了ButtonTextBoxCheckBoxDataGrid等等样式资源代码 有些网友推荐 MVVMLight 或者 Prism 等第三方MVVM框架引用 六、下篇预告 在制作WPF过程中遇到的问题以及解决方案 其中包括焦点的控制键盘事件触发输入框的数字限制异步处理隐藏状态可用状态自定义属性等等... 转载于:https://www.cnblogs.com/yjwlogs/p/8459751.html