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 = "用户名为空";//前台文本框会显示
                }
            }
    }
  • 相关阅读:
    [LOJ#6068]. 「2017 山东一轮集训 Day4」棋盘[费用流]
    [BZOJ4842]Delight for a Cat[费用流]
    [HNOI2018]转盘[结论+线段树]
    [LOJ#6066]. 「2017 山东一轮集训 Day3」第二题[二分+括号序列+hash]
    [CF963E]Circles of Waiting[高斯消元网格图优化+期望]
    [CF966F]May Holidays[分块+虚树]
    【JZOJ5088】【GDOI2017第四轮模拟day2】最小边权和 排序+动态规划
    【JZOJ5086】【GDOI2017第四轮模拟day1】数列 折半搜索
    GDOI2017第四轮day1总结
    【51nod1563】坐标轴上的最大团 贪心
  • 原文地址:https://www.cnblogs.com/JohnnyBao/p/4442097.html
Copyright © 2011-2022 走看看