WPF实战学习笔记03-绑定菜单

绑定菜单

新建文件夹、与文件

  • Common->Models、ViewModel、Views

  • Models 内新建类文件 MenuBars.cs

  • Views内新建XAML文件MainView.xaml

  • MenuBars.cs 添加代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Mytodo.Common.Models
    {
        public class MenuBar
        {
    		private string icon;
    
    		/// 
    		/// 图标字符串
    		/// 
    		public string Icon
    		{
    			get { return icon; }
    			set { icon = value; }
    		}
    
    		private string title;
    
    		public string Title
    		{
    			get { return title; }
    			set { title = value; }
    		}
    
    		private string nameSpace;
    
    		public string  NameSpace
    		{
    			get { return nameSpace; }
    			set { nameSpace = value; }
    		}
    
    
    	}
    }
    
    

移动MainWindow内容

MainWindow内容,移动到Mainview.xaml中,并做相应修改,并把对应的.cs文件复制过来

x:Class="Mytodo.Views.MainView"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Mytodo.Views
{
    /// 
    /// MainView.xaml 的交互逻辑
    /// 
    public partial class MainView : Window
    {
        public MainView()
        {
            InitializeComponent();

            btnclo.Click += (s, e) =>
            {
                this.Close();
            };

            btnmin.Click += (s, e) =>
            {
                this.WindowState = WindowState.Minimized;
            };

            btnmax.Click += (s, e) =>
            {
                this.WindowState = WindowState.Maximized;
            };

            ColorZone.MouseMove += (s, e) =>
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                    this.DragMove();
            };

            ColorZone.MouseDoubleClick += (s, e) =>
            {
                if (WindowState != WindowState.Normal)
                    WindowState = WindowState.Normal;
                else
                    WindowState = WindowState.Maximized;
            };
        }
    }
}

编写菜单数据初始化代码

using Mytodo.Common.Models;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mytodo.ViewModels
{
    public class MainViewModel:BindableBase
    {
        public MainViewModel()
        {
            MenuBars=new ObservableCollection();
            CreatMenuBar();
        }


        private ObservableCollection menuBars;

        public ObservableCollection MenuBars
        {
            get { return menuBars; }
            set { menuBars = value; RaisePropertyChanged(); }
        }

        void CreatMenuBar()
        {
            MenuBars.Add(new MenuBar { Icon = "Home",               NameSpace = "HomeView",     Title = "首页" });
            MenuBars.Add(new MenuBar { Icon = "FormatListChecks",   NameSpace = "TodoView",     Title = "待办事项" });
            MenuBars.Add(new MenuBar { Icon = "Notebook",           NameSpace = "NoteView",     Title = "备忘录" });
            MenuBars.Add(new MenuBar { Icon = "Cog",                NameSpace = "SettingView",  Title = "设置" });
        }
    }
}

注意

  • 图标可以从MaterialDesign里面拿,格式为:

编写XAML界面程序



    
        
            
                
                    
                        
                            
                                
                                    
                                
                            
                            
                        
                        
                            
                                
                                    
                                        
                                        
                                    
                                
                            
                        
                    
                
                
                    
                        
                            

                            
本次中添加

    
        
            
                
                    
                
            
            
        
        
            
                
                    
                        
                        
                    
                
            
        
    

修改启动窗口

在App.xaml.cs中修改如下:

protected override Window CreateShell ()
{
    return Container.Resolve ();
}
更改主题样式
    
        
            
                

其中,BaseTheme=“Dark”

添加ListboxItem的样式

你可能感兴趣的:(WPF实战学习笔记,wpf,学习,笔记)