mvvm sample

main
syneffort 2 years ago
parent b69c06f4c4
commit 0f0a0b1074
  1. 8
      PacticeSolution/MVVMSample/App.xaml
  2. 17
      PacticeSolution/MVVMSample/App.xaml.cs
  3. 10
      PacticeSolution/MVVMSample/AssemblyInfo.cs
  4. 10
      PacticeSolution/MVVMSample/MVVMSample.csproj
  5. 11
      PacticeSolution/MVVMSample/MainWindow.xaml
  6. 28
      PacticeSolution/MVVMSample/MainWindow.xaml.cs
  7. 34
      PacticeSolution/MVVMSample/Model/NotifierMain.cs
  8. 75
      PacticeSolution/MVVMSample/Model/ViewModelMain.cs
  9. 59
      PacticeSolution/MVVMSample/View/ViewMain.xaml
  10. 34
      PacticeSolution/MVVMSample/View/ViewMain.xaml.cs
  11. 6
      PacticeSolution/PacticeSolution.sln

@ -0,0 +1,8 @@
<Application x:Class="MVVMSample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MVVMSample"
StartupUri="View/ViewMain.xaml">
<Application.Resources>
</Application.Resources>
</Application>

@ -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 MVVMSample
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

@ -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)
)]

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>

@ -0,0 +1,11 @@
<Window x:Class="MVVMSample.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:MVVMSample" xmlns:view="clr-namespace:MVVMSample.View"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="500">
<Grid>
</Grid>
</Window>

@ -0,0 +1,28 @@
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 MVVMSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVVMSample.Model
{
internal class NotifierMain : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
public virtual void NotifyChanged(params string[] propertyNames)
{
foreach (string name in propertyNames)
{
OnPropertyChanged(new PropertyChangedEventArgs(name));
}
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, e);
}
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}

@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace MVVMSample.Model
{
internal class ViewModelMain : NotifierMain
{
private string _firstName;
private string _lastName;
private string _fullName;
private string _birthday;
private bool _isBirthdayError = false;
public string FirstName
{
get { return _firstName; }
set
{
_firstName = value;
_fullName = $"{_lastName} {_firstName}";
NotifyChanged("FirstName", "FullName");
}
}
public string LastName
{
get { return _lastName; }
set
{
_lastName = value;
_fullName = $"{_lastName} {_firstName}";
NotifyChanged("LastName", "FullName");
}
}
public string FullName { get { return _fullName; } }
public string Birthday
{
get { return _birthday; }
set
{
if (Regex.IsMatch(value, "^[0-9]*$"))
{
_birthday = value;
_isBirthdayError = false;
}
else
{
_birthday = value;
_isBirthdayError = true;
}
NotifyChanged("Birthday", "BirthdayError", "BirthdayErrorMessage");
}
}
public bool BirthdayError { get { return _isBirthdayError; } }
public string BirthdayErrorMessage
{
get
{
if (!_isBirthdayError)
return $"Birthday: {_birthday}";
else
return $"Please input the number value ({Birthday})";
}
}
}
}

@ -0,0 +1,59 @@
<Window x:Class="MVVMSample.View.ViewMain"
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:MVVMSample.View" xmlns:model="clr-namespace:MVVMSample.Model"
mc:Ignorable="d"
Title="ViewMain" Height="200" Width="400">
<!--<Window.DataContext>
<model:ViewModelMain/>
</Window.DataContext>-->
<Grid>
<Grid.DataContext>
<model:ViewModelMain/>
</Grid.DataContext>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0" VerticalAlignment="Center">Last Name</Label>
<Label Grid.Column="0" Grid.Row="1" VerticalAlignment="Center">First Name</Label>
<Label Grid.Column="0" Grid.Row="2" VerticalAlignment="Center">Birthday(Number)</Label>
<Label Grid.Column="0" Grid.Row="3" VerticalAlignment="Center">Full Name</Label>
<Label Grid.Column="0" Grid.Row="4" VerticalAlignment="Center">Birthday</Label>
<TextBox x:Name="txtLastName" Grid.Column="1" Grid.Row="0" VerticalAlignment="Center" Margin="10,0,10,0"
Text="{Binding LastName, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Name="txtFirstName" Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" Margin="10,0,10,0"
Text="{Binding FirstName, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Name="txtBirthday" Grid.Column="1" Grid.Row="2" VerticalAlignment="Center" Margin="10,0,10,0"
Text="{Binding Birthday, UpdateSourceTrigger=PropertyChanged}"/>
<Label x:Name="lblFullName" Grid.Column="1" Grid.Row="3" VerticalAlignment="Center" Margin="10,0,10,0"
Content="{Binding FullName, UpdateSourceTrigger=PropertyChanged}"/>
<Label x:Name="lblBirthday" Grid.Column="1" Grid.Row="4" VerticalAlignment="Center" Margin="10,0,10,0"
Content="{Binding BirthdayErrorMessage, UpdateSourceTrigger=PropertyChanged}">
<Label.Style>
<Style TargetType="{x:Type Label}">
<Setter Property="Foreground" Value="Black"/>
<Style.Triggers>
<DataTrigger Binding="{Binding BirthdayError}" Value="TRUE">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
</Grid>
</Window>

@ -0,0 +1,34 @@
using MVVMSample.Model;
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 MVVMSample.View
{
/// <summary>
/// ViewMain.xaml에 대한 상호 작용 논리
/// </summary>
public partial class ViewMain : Window
{
public ViewMain()
{
InitializeComponent();
InitInstance();
}
private void InitInstance()
{
//this.DataContext = new ViewModelMain();
}
}
}

@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MapFinder", "MapFinder\MapF
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataBindingSample", "DataBindingSample\DataBindingSample.csproj", "{8A3E4E38-6E95-4955-8BFE-6E58F4AECE27}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MVVMSample", "MVVMSample\MVVMSample.csproj", "{FFD799B6-18C5-413B-8984-B44C803473B0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -33,6 +35,10 @@ Global
{8A3E4E38-6E95-4955-8BFE-6E58F4AECE27}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8A3E4E38-6E95-4955-8BFE-6E58F4AECE27}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8A3E4E38-6E95-4955-8BFE-6E58F4AECE27}.Release|Any CPU.Build.0 = Release|Any CPU
{FFD799B6-18C5-413B-8984-B44C803473B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FFD799B6-18C5-413B-8984-B44C803473B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FFD799B6-18C5-413B-8984-B44C803473B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FFD799B6-18C5-413B-8984-B44C803473B0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

Loading…
Cancel
Save