mvvm sample (advanced)
This commit is contained in:
9
PacticeSolution/MVVMDatabaseSample/App.xaml
Normal file
9
PacticeSolution/MVVMDatabaseSample/App.xaml
Normal file
@@ -0,0 +1,9 @@
|
||||
<Application x:Class="MVVMDatabaseSample.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:MVVMDatabaseSample"
|
||||
StartupUri="MainWindow.xaml">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
</Application>
|
17
PacticeSolution/MVVMDatabaseSample/App.xaml.cs
Normal file
17
PacticeSolution/MVVMDatabaseSample/App.xaml.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace MVVMDatabaseSample
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for App.xaml
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
}
|
||||
}
|
10
PacticeSolution/MVVMDatabaseSample/AssemblyInfo.cs
Normal file
10
PacticeSolution/MVVMDatabaseSample/AssemblyInfo.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System.Windows;
|
||||
|
||||
[assembly: ThemeInfo(
|
||||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
|
||||
//(used if a resource is not found in the page,
|
||||
// or application resource dictionaries)
|
||||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
|
||||
//(used if a resource is not found in the page,
|
||||
// app, or any theme specific resource dictionaries)
|
||||
)]
|
18
PacticeSolution/MVVMDatabaseSample/MVVMDatabaseSample.csproj
Normal file
18
PacticeSolution/MVVMDatabaseSample/MVVMDatabaseSample.csproj
Normal file
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net6.0-windows</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<UseWPF>true</UseWPF>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="View\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Data.SqlClient" Version="4.8.5" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
40
PacticeSolution/MVVMDatabaseSample/MainWindow.xaml
Normal file
40
PacticeSolution/MVVMDatabaseSample/MainWindow.xaml
Normal file
@@ -0,0 +1,40 @@
|
||||
<Window x:Class="MVVMDatabaseSample.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:MVVMDatabaseSample" xmlns:viewmodel="clr-namespace:MVVMDatabaseSample.ViewModel"
|
||||
mc:Ignorable="d"
|
||||
Title="MainWindow" Height="350" Width="500">
|
||||
|
||||
<Window.DataContext>
|
||||
<viewmodel:ViewModelMain/>
|
||||
</Window.DataContext>
|
||||
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*"/>
|
||||
<ColumnDefinition Width="150"/>
|
||||
<ColumnDefinition Width="200"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="*"/>
|
||||
<RowDefinition Height="70"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<DataGrid x:Name="grdMain" Grid.ColumnSpan="3" Margin="5"
|
||||
AutoGenerateColumns="False"
|
||||
ItemsSource="{Binding SampleData, Mode=TwoWay, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}">
|
||||
<DataGrid.Columns>
|
||||
<DataGridTextColumn MinWidth="10" Width="Auto" Header="Name" Binding="{Binding Name}"/>
|
||||
<DataGridTextColumn MinWidth="100" Width="Auto" Header="AGE" Binding="{Binding Age}"/>
|
||||
<DataGridTextColumn MinWidth="100" Width="Auto" Header="GRADE" Binding="{Binding Grade}"/>
|
||||
<DataGridTextColumn MinWidth="200" Width="*" Header="SCORE" Binding="{Binding Score}"/>
|
||||
</DataGrid.Columns>
|
||||
</DataGrid>
|
||||
|
||||
<Button Grid.Column="0" Grid.Row="1" Margin="5" Content="Connect" Command="{Binding ConnectCommand}"/>
|
||||
<Button Grid.Column="1" Grid.Row="1" Margin="5" Content="Search" Command="{Binding SelectCommand}"/>
|
||||
<Button Grid.Column="2" Grid.Row="1" Margin="5" Content="Update" IsEnabled="False" Command="{Binding Update}"/>
|
||||
</Grid>
|
||||
</Window>
|
35
PacticeSolution/MVVMDatabaseSample/MainWindow.xaml.cs
Normal file
35
PacticeSolution/MVVMDatabaseSample/MainWindow.xaml.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using MVVMDatabaseSample.Util;
|
||||
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.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace MVVMDatabaseSample
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for MainWindow.xaml
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
InitInstance();
|
||||
}
|
||||
|
||||
private void InitInstance()
|
||||
{
|
||||
SqlDBManager.Instance.ConnectionString = "Data Source=peacecloud.synology.me,21433;Initial Catalog=Study;User ID=study;Password=Study1234";
|
||||
}
|
||||
}
|
||||
}
|
16
PacticeSolution/MVVMDatabaseSample/Model/Student.cs
Normal file
16
PacticeSolution/MVVMDatabaseSample/Model/Student.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MVVMDatabaseSample.Model
|
||||
{
|
||||
internal class Student
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public int Age { get; set; }
|
||||
public string Grade { get; set; }
|
||||
public int Score { get; set; }
|
||||
}
|
||||
}
|
54
PacticeSolution/MVVMDatabaseSample/Util/SqlDBManager.cs
Normal file
54
PacticeSolution/MVVMDatabaseSample/Util/SqlDBManager.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MVVMDatabaseSample.Util
|
||||
{
|
||||
internal sealed class SqlDBManager
|
||||
{
|
||||
public static SqlDBManager Instance { get; private set; } = new SqlDBManager();
|
||||
|
||||
public string ConnectionString { get; set; }
|
||||
|
||||
public SqlConnection Connection { get; private set; }
|
||||
|
||||
private SqlCommand _sqlCommand;
|
||||
|
||||
public bool GetConnection()
|
||||
{
|
||||
try
|
||||
{
|
||||
_sqlCommand = new SqlCommand();
|
||||
_sqlCommand.Connection = this.Connection;
|
||||
|
||||
this.Connection = new SqlConnection(ConnectionString);
|
||||
this.Connection.Open();
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public DataSet ExecuteQuery(DataSet ds, string query)
|
||||
{
|
||||
ds.Reset();
|
||||
lock (this)
|
||||
{
|
||||
SqlDataAdapter adapter = new SqlDataAdapter();
|
||||
adapter.SelectCommand = _sqlCommand;
|
||||
adapter.SelectCommand.Connection = this.Connection;
|
||||
adapter.SelectCommand.CommandText = query;
|
||||
adapter.Fill(ds);
|
||||
}
|
||||
|
||||
return ds;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace MVVMDatabaseSample.ViewModel
|
||||
{
|
||||
internal class DelegateCommand : ICommand
|
||||
{
|
||||
private readonly Func<bool> _canExecute;
|
||||
private readonly Action _execute;
|
||||
|
||||
public event EventHandler? CanExecuteChanged;
|
||||
|
||||
public DelegateCommand(Action execute, Func<bool> canExecute)
|
||||
{
|
||||
_execute = execute;
|
||||
_canExecute = canExecute;
|
||||
}
|
||||
|
||||
public DelegateCommand(Action execute) : this(execute, null)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public bool CanExecute(object? parameter)
|
||||
{
|
||||
if (_canExecute == null)
|
||||
return true;
|
||||
|
||||
return _canExecute();
|
||||
}
|
||||
|
||||
public void Execute(object? parameter)
|
||||
{
|
||||
_execute();
|
||||
}
|
||||
|
||||
public void RaiseCanExecuteChanged()
|
||||
{
|
||||
if (this.CanExecuteChanged != null)
|
||||
this.CanExecuteChanged(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
}
|
160
PacticeSolution/MVVMDatabaseSample/ViewModel/ViewModelMain.cs
Normal file
160
PacticeSolution/MVVMDatabaseSample/ViewModel/ViewModelMain.cs
Normal file
@@ -0,0 +1,160 @@
|
||||
using MVVMDatabaseSample.Model;
|
||||
using MVVMDatabaseSample.Util;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace MVVMDatabaseSample.ViewModel
|
||||
{
|
||||
internal class ViewModelMain : INotifyPropertyChanged
|
||||
{
|
||||
private Student _student = new Student();
|
||||
private ObservableCollection<Student> _sampleData;
|
||||
private ICommand _connectCommand;
|
||||
private ICommand _selectCommand;
|
||||
private ICommand _loadedCommand;
|
||||
|
||||
public event PropertyChangedEventHandler? PropertyChanged;
|
||||
|
||||
public string Name
|
||||
{
|
||||
get { return _student.Name; }
|
||||
set
|
||||
{
|
||||
_student.Name = value;
|
||||
OnPropertyChanged("Name");
|
||||
}
|
||||
}
|
||||
|
||||
public int Age
|
||||
{
|
||||
get { return _student.Age; }
|
||||
set
|
||||
{
|
||||
_student.Age = value;
|
||||
OnPropertyChanged("Age");
|
||||
}
|
||||
}
|
||||
|
||||
public string Grage
|
||||
{
|
||||
get { return _student.Grade; }
|
||||
set
|
||||
{
|
||||
_student.Grade = value;
|
||||
OnPropertyChanged("Grade");
|
||||
}
|
||||
}
|
||||
|
||||
public int Score
|
||||
{
|
||||
get { return _student.Score; }
|
||||
set
|
||||
{
|
||||
_student.Score = value;
|
||||
OnPropertyChanged("Score");
|
||||
}
|
||||
}
|
||||
|
||||
public ObservableCollection<Student> SampleData
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_sampleData == null)
|
||||
_sampleData = new ObservableCollection<Student>();
|
||||
|
||||
return _sampleData;
|
||||
}
|
||||
set { _sampleData = value; }
|
||||
}
|
||||
|
||||
public ICommand ConnectCommand
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_connectCommand == null)
|
||||
_connectCommand = new DelegateCommand(Connect);
|
||||
|
||||
return _connectCommand;
|
||||
}
|
||||
}
|
||||
|
||||
public ICommand SelectCommand
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_selectCommand == null)
|
||||
_selectCommand = new DelegateCommand(Select);
|
||||
|
||||
return _selectCommand;
|
||||
}
|
||||
}
|
||||
|
||||
public ICommand LoadedCommand
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_loadedCommand == null)
|
||||
_loadedCommand = new DelegateCommand(Load);
|
||||
|
||||
return _loadedCommand;
|
||||
}
|
||||
}
|
||||
|
||||
private void Connect()
|
||||
{
|
||||
string message;
|
||||
if (!SqlDBManager.Instance.GetConnection())
|
||||
message = "Fail to connect";
|
||||
else
|
||||
message = "Success to connect";
|
||||
|
||||
MessageBox.Show(message);
|
||||
}
|
||||
|
||||
private void Select()
|
||||
{
|
||||
DataSet ds = new DataSet();
|
||||
string qry = "SELECT * FROM Student";
|
||||
SqlDBManager.Instance.ExecuteQuery(ds, qry);
|
||||
if (ds.Tables.Count < 1)
|
||||
return;
|
||||
|
||||
foreach (DataRow row in ds.Tables[0].Rows)
|
||||
{
|
||||
Student student = new Student()
|
||||
{
|
||||
Name = row["Name"].ToString(),
|
||||
Age = (int)row["Age"],
|
||||
Grade = row["Grade"].ToString(),
|
||||
Score = (int)row["Score"],
|
||||
};
|
||||
_sampleData.Add(student);
|
||||
}
|
||||
}
|
||||
|
||||
private void Load()
|
||||
{
|
||||
string message;
|
||||
if (!SqlDBManager.Instance.GetConnection())
|
||||
message = "Fail to connect";
|
||||
else
|
||||
message = "Success to connect";
|
||||
|
||||
MessageBox.Show(message);
|
||||
}
|
||||
|
||||
protected void OnPropertyChanged(string propertyName)
|
||||
{
|
||||
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
}
|
@@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MVVMSample", "MVVMSample\MV
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ResourceSample", "ResourceSample\ResourceSample.csproj", "{858EC308-9ABD-44C1-92E1-59911F60FEB9}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MVVMDatabaseSample", "MVVMDatabaseSample\MVVMDatabaseSample.csproj", "{B0A8E6DE-C6A2-488E-A148-9E7812F615F2}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -45,6 +47,10 @@ Global
|
||||
{858EC308-9ABD-44C1-92E1-59911F60FEB9}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{858EC308-9ABD-44C1-92E1-59911F60FEB9}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{858EC308-9ABD-44C1-92E1-59911F60FEB9}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{B0A8E6DE-C6A2-488E-A148-9E7812F615F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{B0A8E6DE-C6A2-488E-A148-9E7812F615F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{B0A8E6DE-C6A2-488E-A148-9E7812F615F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{B0A8E6DE-C6A2-488E-A148-9E7812F615F2}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@@ -31,7 +31,7 @@ namespace ResourceSample
|
||||
if (_clickCount % 2 == 0)
|
||||
this.Resources["CustomEventColor"] = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#3de385"));
|
||||
else
|
||||
this.Resources["CustomEventColor"] = Resources["CustomRed"];
|
||||
this.Resources["CustomEventColor"] = Application.Current.FindResource("CustomRed");
|
||||
|
||||
_clickCount++;
|
||||
}
|
||||
|
Reference in New Issue
Block a user