zoukankan      html  css  js  c++  java
  • MVVM初步搭建应用

    MVVM模式:
    利用 prism Microsoft.Practices.Prism.dll
    WPF Interaction框架简介 添加Interactions库的引用。主要添加如下两个DLL:
    Microsoft.Expression.Interactions.dll和System.Windows.Interactivity.dll(一般系统自带),像Load时候的command
    要先引用
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:ig="http://schemas.infragistics.com/xaml"

    调用方式:

    <i:Interaction.Triggers>
            <i:EventTrigger>
                <i:InvokeCommandAction Command="{Binding LoadCommand}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    <!--ComboBox绑定下拉列表-->
    <ComboBox x:Name="cbYear" ItemsSource="{Binding 
    
    Path=YearList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding Path=SelectedYear}">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="SelectionChanged" SourceObject="{Binding 
    
    ElementName=cbYear}">
                                    <i:InvokeCommandAction Command="{Binding YearChangedCommand}" 
    
    CommandName="YearChangedCommand"/>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </ComboBox>

    绑定按钮用  Command 如Command="{Binding LoginCommand}"

    在设计界面:<Button Content="登录" Name="btn_Login" Command="{Binding LoginCommand}"></Button>

    在cs也可以写:

    this.btn_Login.Click += new RoutedEventHandler(btn_Login_Click);
    MainWindowViewModel viewModel = new MainWindowViewModel();
    void btn_Login_Click(object sender, RoutedEventArgs e)
    {
    viewModel.LoginCommand.Execute();
    }

    接下来就要写view对应的ViewModel了 MainWindowViewModel,如果需要绑定界面值,如textbox文本框,能够动态显示值
    ,这个需要用到Prism,类需要集成NotificationObject

    public class MainWindowViewModel : NotificationObject
        {
            private string _userName= string.Empty;
            public string UserName
            {
                get { return _userName; }
                set
                {
                    _userName = value;
                    RaisePropertyChanged("UserName");
                }
            }
        //定义Command
            public DelegateCommand LoadCommnad { get; set; }
     public MainWindowViewModel()
            {
                this.LoginCommand = new DelegateCommand(Login);//Command调用Login方法
            }
    //响应Command
     private void Login()
            {
                if (string.IsNullOrEmpty(UserName))
                {
                    UserName = "用户名为空";//前台文本框会显示
                }
            }
    }
  • 相关阅读:
    适配器模式
    自己DIY word2010脚注和尾注没有的格式
    Linux单网卡,双IP,双网关配置,并搭建squid proxy上网
    about using gnuplot
    ReadDirectoryChangesW 函数 流沙
    Jquery easyui 异步树 流沙
    Overlapped I/O 学习 流沙
    jQuery.get(url,[data],[callback]) 流沙
    MsgWaitForMultipleObjectsEx用法 流沙
    Oracle smon_scn_time 表 说明
  • 原文地址:https://www.cnblogs.com/JohnnyBao/p/4442097.html
Copyright © 2011-2022 走看看