原创不易,打字不易,截图不易,多多点赞,送人玫瑰,留有余香,财务自由明日实现
1、创建用户管理的用户控件
<UserControl x:Class="West.StoreMgr.View.UserInfoView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:West.StoreMgr.View"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:converter="clr-namespace:West.StoreMgr.Helper.Converter"
mc:Ignorable="d"
DataContext="{Binding Source={StaticResource Locator},Path=UserInfo}"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<converter:NumberToRoleConverter x:Key="NumberToRoleConverter"/>
</UserControl.Resources>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding LoadCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<!--标题-->
<StackPanel Background="#EDF0F6" Orientation="Horizontal">
<TextBlock Margin="10 0 0 0" Text="" FontSize="20" FontFamily="/Fonts/#FontAwesome" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="#797672"/>
<TextBlock Margin="10 0 0 0" Text="首页 > 用户管理" FontSize="20" FontFamily="/Fonts/#FontAwesome" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="#797672"/>
</StackPanel>
<!--增加-->
<Grid Grid.Row="1" Margin="7">
<Grid.RowDefinitions>
<RowDefinition Height="45"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Border Background="#72BBE5">
<TextBlock Text="添加用户" FontSize="18" VerticalAlignment="Center" Foreground="#1F3C4C" Margin="0 0 10 0"/>
</Border>
<StackPanel Grid.Row="1" Orientation="Vertical" VerticalAlignment="Center" Margin="0 10 0 10">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="0 10 0 10">
<TextBlock Margin="0 0 10 0" Text=" 用户名:" VerticalAlignment="Center" Width="80"/>
<TextBox HorizontalAlignment="Left" VerticalAlignment="Center" TextAlignment="Left" VerticalContentAlignment="Center" Margin="0 0 10 0" Text="{Binding UserInfo.Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="150" Height="30" />
<TextBlock Margin="0 0 10 0" Text="密码:" VerticalAlignment="Center" Width="80"/>
<TextBox HorizontalAlignment="Left" VerticalAlignment="Center" TextAlignment="Left" VerticalContentAlignment="Center" Margin="0 0 10 0" Text="{Binding UserInfo.Password,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="150" Height="30" />
<TextBlock Margin="0 0 10 0" Text="角色:" VerticalAlignment="Center" Width="80"/>
<ComboBox SelectedItem="{Binding Role,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectedIndex="0" MinWidth="150" IsReadOnly="False">
<ComboBoxItem Content="管理员" Tag="0"/>
<ComboBoxItem Content="普通用户" Tag="1"/>
</ComboBox>
</StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="0 10 0 10">
<TextBlock Margin="0 0 10 0" Text="电话:" VerticalAlignment="Center" Width="80"/>
<TextBox HorizontalAlignment="Left" VerticalAlignment="Center" TextAlignment="Left" VerticalContentAlignment="Center" Margin="0 0 10 0" Text="{Binding UserInfo.Telephone,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="150" Height="30" />
<TextBlock Margin="0 0 10 0" Text="邮箱:" VerticalAlignment="Center" Width="80"/>
<TextBox HorizontalAlignment="Left" VerticalAlignment="Center" TextAlignment="Left" VerticalContentAlignment="Center" Margin="0 0 10 0" Text="{Binding UserInfo.Email,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="150" Height="30" />
<TextBlock Margin="0 0 10 0" Text="地址:" VerticalAlignment="Center" Width="80"/>
<TextBox HorizontalAlignment="Left" VerticalAlignment="Center" TextAlignment="Left" VerticalContentAlignment="Center" Margin="0 0 10 0" Text="{Binding UserInfo.Address,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="150" Height="30" />
</StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="0 10 0 14">
<TextBlock Margin="0 0 10 0" Text="备注:" VerticalAlignment="Center" Width="80"/>
<TextBox HorizontalAlignment="Left" VerticalAlignment="Center" TextAlignment="Left" VerticalContentAlignment="Center" Margin="0 0 10 0" Text="{Binding UserInfo.Tag,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="400" Height="30" />
<Button Margin="40 0 0 0" Height="36" Width="199" FontSize="20"
Content="增 加" Style="{StaticResource ButtonStyle}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local:UserInfoView}}"
Command="{Binding AddCommand}"/>
</StackPanel>
</StackPanel>
</Grid>
<!--浏览-->
<Grid Grid.Row="2" Margin="7">
<DataGrid ItemsSource="{Binding UserInfoList}" CanUserDeleteRows="False" CanUserAddRows="False" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="序号" Binding="{Binding Id}"/>
<DataGridTextColumn Header="用户名" Binding="{Binding Name}"/>
<DataGridTextColumn Header="密码" Binding="{Binding Password}"/>
<DataGridTextColumn Header="角色" Binding="{Binding Role,Converter={StaticResource NumberToRoleConverter}}"/>
<DataGridTextColumn Header="电话" Binding="{Binding Telephone}"/>
<DataGridTextColumn Header="邮箱" Binding="{Binding Email}"/>
<DataGridTextColumn Header="地址" Binding="{Binding Address}" IsReadOnly="True"/>
<DataGridTextColumn Header="日期" Binding="{Binding InsertDate}"/>
<DataGridTextColumn Header="备注" Binding="{Binding Tag}" IsReadOnly="True"/>
<DataGridTemplateColumn Header="操作">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Content="编辑"
Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local:UserInfoView},Path=DataContext.EditCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}}"
Tag="{Binding}"
Style="{StaticResource DataGridButtonStyle}" />
<Button Content="删除"
Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local:UserInfoView},Path=DataContext.DeleteCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}}"
Tag="{Binding}"
Style="{StaticResource DataGridButtonStyle}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Grid>
</UserControl>
2、创建用户管理viewmodel
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using West.StoreMgr.Helper;
using West.StoreMgr.View;
using West.StoreMgr.Service;
using static West.StoreMgr.Windows.MsgBoxWindow;
using West.StoreMgr.Windows;
using CommonServiceLocator;
namespace West.StoreMgr.ViewModel
{
/// <summary>
/// 用户管理viewmodel
/// </summary>
public class UserInfoViewModel : ViewModelBase
{
private UserInfo userInfo = new UserInfo();
public UserInfo UserInfo
{
get { return userInfo; }
set { userInfo = value; RaisePropertyChanged(); }
}
private List<UserInfo> userInfoList = new List<UserInfo>();
public List<UserInfo> UserInfoList
{
get { return userInfoList; }
set { userInfoList = value; RaisePropertyChanged(); }
}
private ComboBoxItem role = null;
public ComboBoxItem Role
{
get { return role; }
set { role = value; RaisePropertyChanged(); }
}
public RelayCommand LoadCommand
{
get
{
return new RelayCommand(() =>
{
UserInfoList = new UserInfoService().Select();
});
}
}
/// <summary>
/// 修改
/// </summary>
public RelayCommand<Button> EditCommand
{
get
{
var command = new RelayCommand<Button>((view) =>
{
var old = view.Tag as UserInfo;
if (old == null) return;
var model = ServiceLocator.Current.GetInstance<EditUserInfoViewModel>();
model.User = old;
var window = new EditUserInfoWindow();
window.ShowDialog();
UserInfoList = new UserInfoService().Select();
});
return command;
}
}
//添加
public RelayCommand<UserControl> AddCommand
{
get
{
var command = new RelayCommand<UserControl>((obj) =>
{
if (!(obj is UserInfoView view)) return;
if (string.IsNullOrEmpty(UserInfo.Name) == true
|| string.IsNullOrEmpty(UserInfo.Password) == true)
{
MsgWinHelper.ShowError("用户名和密码不能为空");
return;
}
UserInfo.InsertDate = DateTime.Now;
if (Role == null) return;
if (int.TryParse(Role.Tag.ToString(), out int result))
{
UserInfo.Role = result;
}
else
{
return;
}
var service = new UserInfoService();
int count = service.Insert(UserInfo);
if (count > 0)
{
UserInfoList = service.Select();
MsgWinHelper.ShowMessage("操作成功");
UserInfo = new UserInfo();
}
else
{
MsgWinHelper.ShowMessage("操作失败");
}
});
return command;
}
}
//删除
public RelayCommand<Button> DeleteCommand
{
get
{
var command = new RelayCommand<Button>((view) =>
{
var old = view.Tag as UserInfo;
if (old == null) return;
if (old.Name == "admin")
{
MsgWinHelper.ShowError("系统管理员不能删除!");
return;
}
if (MsgWinHelper.ShowQuestion("您确定要删除该用户吗?") == CustomMessageBoxResult.OK)
{
var service = new UserInfoService();
int count = service.Delete(old);
if (count > 0)
{
UserInfoList = service.Select();
MsgWinHelper.ShowMessage("操作成功");
}
else
{
MsgWinHelper.ShowMessage("操作失败");
}
}
});
return command;
}
}
}
}
3、运行效果