珂珂的个人博客 - 一个程序猿的个人网站

WPF的MVVM实例(五)-编辑页

[原]WPF的MVVM实例(一)

[原]WPF的MVVM实例(二)-ViewModel基类

[原]WPF的MVVM实例(三)-ViewModel实现类

[原]WPF的MVVM实例(四)-列表页

[原]WPF的MVVM实例(五)-编辑页

XAML代码

<Window x:Class="MyList.UI.RegisterEditWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MyList.UI"
        xmlns:entity="clr-namespace:MyList.Entity;assembly=MyList.Entity"
        xmlns:util="clr-namespace:MyList.UI.Util"
        Title="编辑注册记录" Height="430" Width="300" x:Name="window1">

    <Window.Resources>
       
    </Window.Resources>
    <StackPanel DataContext="{Binding SaveData}">

        <WrapPanel  Margin="10,30,0,0">
            <TextBlock Text="网站名称" Width="50"></TextBlock>
            <TextBox  Width="180" Height="21"  Margin="10,0,0,0" >
                <Binding Path="registerUrlName"  UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True"  NotifyOnValidationError="True"  ValidatesOnExceptions="True">
                </Binding>
                <TextBox.ToolTip>
                    <Binding RelativeSource="{RelativeSource Self}" Path="(Validation.Errors)[0].ErrorContent"></Binding>
                </TextBox.ToolTip>
            </TextBox>
        </WrapPanel>
        <WrapPanel Margin="10,10,0,0">
            <TextBlock Text="网站地址" Width="50"></TextBlock>
            <TextBox Width="180" Height="21" Margin="10,0,0,0" Text="{Binding registerUrl, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, ValidatesOnDataErrors=True, NotifyOnValidationError=True}">
                <TextBox.ToolTip>
                    <Binding RelativeSource="{RelativeSource Self}" Path="(Validation.Errors)[0].ErrorContent"></Binding>
                </TextBox.ToolTip>
            </TextBox>
        </WrapPanel>
        <WrapPanel Margin="10,10,0,0">
            <TextBlock Text="用户名" Width="50"></TextBlock>
            <TextBox  Width="180" Height="21" Margin="10,0,0,0" Text="{Binding registerUserName}"></TextBox>
        </WrapPanel>
        <WrapPanel Margin="10,10,0,0">
            <TextBlock Text="密码" Width="50"></TextBlock>
            <TextBox  Width="180" Height="21" Margin="10,0,0,0"  Text="{Binding registerUserPassword}"></TextBox>
        </WrapPanel>
        <WrapPanel Margin="10,10,0,0">
            <TextBlock Text="邮箱" Width="50"></TextBlock>
            <TextBox  Width="180" Height="21" Margin="10,0,0,0" Text="{Binding registerEmail}"></TextBox>
        </WrapPanel>
        <WrapPanel Margin="10,10,0,0">
            <TextBlock Text="绑定电话" Width="50"></TextBlock>
            <TextBox  Width="180" Height="21" Margin="10,0,0,0" Text="{Binding bindTel}"></TextBox>
        </WrapPanel>
        <WrapPanel Margin="10,10,0,0">
            <TextBlock Text="绑定QQ" Width="50"></TextBlock>
            <TextBox  Width="180" Height="21" Margin="10,0,0,0" Text="{Binding bindQQ}"></TextBox>
        </WrapPanel>
        <WrapPanel Margin="10,10,0,0">
            <TextBlock Text="绑定微博" Width="50"></TextBlock>
            <TextBox  Width="180" Height="21" Margin="10,0,0,0" Text="{Binding bindWeibo}"></TextBox>
        </WrapPanel>
        <WrapPanel Margin="10,10,0,0">
            <TextBlock Text="备注" Width="50"></TextBlock>
            <TextBox TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" Width="180" Height="50" Margin="10,0,0,0" Text="{Binding movieRemark}"></TextBox>
        </WrapPanel>
       
        <Button Content="添加/修改"   HorizontalAlignment="Center" Margin="0,20,0,0" x:Name="btnSubmit"  Click="btnSubmit_Click"/>
        <!--<TextBlock Text="{Binding Source={StaticResource ResourceKey=Test}, Mode=OneWay}"></TextBlock>-->
    </StackPanel>
</Window>

很少的后台代码

using MyList.Model;
using MyList.ViewModel;
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 MyList.UI
{
    /// <summary>
    /// RegisterEditWindow.xaml 的交互逻辑
    /// </summary>
    public partial class RegisterEditWindow : Window
    {
        public RegisterViewModel Model
        {
            get;
            set;
        }

        public RegisterEditWindow(RegisterViewModel model)
        {
            InitializeComponent();
            if (model.SaveData == null)
            {
                model.SaveData = new System.Collections.ObjectModel.ObservableCollection<RegisterModel>();
                model.SaveData.Add(new RegisterModel());
            }
            this.Model = model;
            this.DataContext = model;
        }

        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            RegisterModel register = this.Model.SaveData.First();
            if(!register.IsValid)
            {
                MessageBox.Show("验证失败:"+register.Error);
                return;
            }
            register.userID = TempData.UserID;
            this.Model.SaveCommand.Execute(null);
            this.DialogResult = true;
        }
    }
}



上一篇:wordpress3.6 修改的地方

下一篇:个人代码全部开源


1 评论

   2014/5/31 1:19:34     58.253.223....


查看所有评论

给个评论吧