From e3106beddeefc071dc4b8846d0b89f4c89d8272c Mon Sep 17 00:00:00 2001 From: syneffort Date: Tue, 23 Apr 2024 14:03:28 +0900 Subject: [PATCH] Converter --- MVVMwithWPF/MVVMwithWPF/App.xaml | 2 +- .../Converters/BoolToStringConverter.cs | 32 ++++++++++++ .../Converters/InvertedBooleanConverter.cs | 26 ++++++++++ .../ParameterContainValueConverter.cs | 33 ++++++++++++ MVVMwithWPF/MVVMwithWPF/Models/Person.cs | 1 + .../ViewModels/ConverterViewModel.cs | 32 ++++++++++++ .../MVVMwithWPF/Views/ConverterWindoew.xaml | 51 +++++++++++++++++++ .../Views/ConverterWindoew.xaml.cs | 27 ++++++++++ 8 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 MVVMwithWPF/MVVMwithWPF/Converters/BoolToStringConverter.cs create mode 100644 MVVMwithWPF/MVVMwithWPF/Converters/InvertedBooleanConverter.cs create mode 100644 MVVMwithWPF/MVVMwithWPF/Converters/ParameterContainValueConverter.cs create mode 100644 MVVMwithWPF/MVVMwithWPF/ViewModels/ConverterViewModel.cs create mode 100644 MVVMwithWPF/MVVMwithWPF/Views/ConverterWindoew.xaml create mode 100644 MVVMwithWPF/MVVMwithWPF/Views/ConverterWindoew.xaml.cs diff --git a/MVVMwithWPF/MVVMwithWPF/App.xaml b/MVVMwithWPF/MVVMwithWPF/App.xaml index ed413cd..54ba07c 100644 --- a/MVVMwithWPF/MVVMwithWPF/App.xaml +++ b/MVVMwithWPF/MVVMwithWPF/App.xaml @@ -2,6 +2,6 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:MVVMwithWPF" - StartupUri="/Views/BindingSampleWithoutViewModelWindow.xaml"> + StartupUri="/Views/ConverterWindoew.xaml"> diff --git a/MVVMwithWPF/MVVMwithWPF/Converters/BoolToStringConverter.cs b/MVVMwithWPF/MVVMwithWPF/Converters/BoolToStringConverter.cs new file mode 100644 index 0000000..5d63517 --- /dev/null +++ b/MVVMwithWPF/MVVMwithWPF/Converters/BoolToStringConverter.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Data; + +namespace MVVMwithWPF.Converters +{ + class BoolToStringConverter : IValueConverter + { + private const string TRUE_VALUE = "남자"; + private const string FALSE_VALUE = "여자"; + + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is bool b) + return b ? TRUE_VALUE : FALSE_VALUE; + + return Binding.DoNothing; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is string s) + return s == TRUE_VALUE ? true : false; + + return Binding.DoNothing; + } + } +} diff --git a/MVVMwithWPF/MVVMwithWPF/Converters/InvertedBooleanConverter.cs b/MVVMwithWPF/MVVMwithWPF/Converters/InvertedBooleanConverter.cs new file mode 100644 index 0000000..2e29a03 --- /dev/null +++ b/MVVMwithWPF/MVVMwithWPF/Converters/InvertedBooleanConverter.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Data; + +namespace MVVMwithWPF.Converters +{ + public class InvertedBooleanConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is bool b) + return !b; + + return Binding.DoNothing; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} diff --git a/MVVMwithWPF/MVVMwithWPF/Converters/ParameterContainValueConverter.cs b/MVVMwithWPF/MVVMwithWPF/Converters/ParameterContainValueConverter.cs new file mode 100644 index 0000000..e71e9bf --- /dev/null +++ b/MVVMwithWPF/MVVMwithWPF/Converters/ParameterContainValueConverter.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Data; + +namespace MVVMwithWPF.Converters +{ + class ParameterContainValueConverter : IValueConverter + { + public bool TrueValue { get; set; } = true; + public bool FalseValue { get; set; } = false; + + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + bool returnValue = FalseValue; + string data = value?.ToString() ?? ""; + if (parameter is not string para) + return returnValue; + + string[] split = para.Split("|"); + returnValue = split.Contains(data) ? TrueValue : FalseValue; + return returnValue; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} diff --git a/MVVMwithWPF/MVVMwithWPF/Models/Person.cs b/MVVMwithWPF/MVVMwithWPF/Models/Person.cs index 8687833..5ecbd40 100644 --- a/MVVMwithWPF/MVVMwithWPF/Models/Person.cs +++ b/MVVMwithWPF/MVVMwithWPF/Models/Person.cs @@ -12,5 +12,6 @@ namespace MVVMwithWPF.Models public int Id { get; set; } public string Name { get; set; } public bool Sex { get; set; } + public string DepartmentCode { get; set; } } } diff --git a/MVVMwithWPF/MVVMwithWPF/ViewModels/ConverterViewModel.cs b/MVVMwithWPF/MVVMwithWPF/ViewModels/ConverterViewModel.cs new file mode 100644 index 0000000..896b25d --- /dev/null +++ b/MVVMwithWPF/MVVMwithWPF/ViewModels/ConverterViewModel.cs @@ -0,0 +1,32 @@ +using CommunityToolkit.Mvvm.ComponentModel; +using MVVMwithWPF.Models; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MVVMwithWPF.ViewModels +{ + public partial class ConverterViewModel : ObservableObject + { + [ObservableProperty] + private ObservableCollection _person; + + [ObservableProperty] + private bool _isEdit; + + public ConverterViewModel() + { + Person = new ObservableCollection() + { + new Person() { Id = 1, Name = "user001", Sex = true, DepartmentCode = "01"}, + new Person() { Id = 2, Name = "user002", Sex = false, DepartmentCode = "02"}, + new Person() { Id = 3, Name = "user003", Sex = true, DepartmentCode = "03"}, + new Person() { Id = 4, Name = "user004", Sex = false, DepartmentCode = "02"}, + new Person() { Id = 5, Name = "user005", Sex = true, DepartmentCode = "01"}, + }; + } + } +} diff --git a/MVVMwithWPF/MVVMwithWPF/Views/ConverterWindoew.xaml b/MVVMwithWPF/MVVMwithWPF/Views/ConverterWindoew.xaml new file mode 100644 index 0000000..00528ed --- /dev/null +++ b/MVVMwithWPF/MVVMwithWPF/Views/ConverterWindoew.xaml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/MVVMwithWPF/MVVMwithWPF/Views/ConverterWindoew.xaml.cs b/MVVMwithWPF/MVVMwithWPF/Views/ConverterWindoew.xaml.cs new file mode 100644 index 0000000..171db90 --- /dev/null +++ b/MVVMwithWPF/MVVMwithWPF/Views/ConverterWindoew.xaml.cs @@ -0,0 +1,27 @@ +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 MVVMwithWPF.Views +{ + /// + /// ConverterWindoew.xaml에 대한 상호 작용 논리 + /// + public partial class ConverterWindoew : Window + { + public ConverterWindoew() + { + InitializeComponent(); + } + } +}