main
syneffort 1 year ago
parent ff88a03af3
commit e3106bedde
  1. 2
      MVVMwithWPF/MVVMwithWPF/App.xaml
  2. 32
      MVVMwithWPF/MVVMwithWPF/Converters/BoolToStringConverter.cs
  3. 26
      MVVMwithWPF/MVVMwithWPF/Converters/InvertedBooleanConverter.cs
  4. 33
      MVVMwithWPF/MVVMwithWPF/Converters/ParameterContainValueConverter.cs
  5. 1
      MVVMwithWPF/MVVMwithWPF/Models/Person.cs
  6. 32
      MVVMwithWPF/MVVMwithWPF/ViewModels/ConverterViewModel.cs
  7. 51
      MVVMwithWPF/MVVMwithWPF/Views/ConverterWindoew.xaml
  8. 27
      MVVMwithWPF/MVVMwithWPF/Views/ConverterWindoew.xaml.cs

@ -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">
<Application.Resources />
</Application>

@ -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;
}
}
}

@ -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();
}
}
}

@ -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();
}
}
}

@ -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; }
}
}

@ -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> _person;
[ObservableProperty]
private bool _isEdit;
public ConverterViewModel()
{
Person = new ObservableCollection<Person>()
{
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"},
};
}
}
}

@ -0,0 +1,51 @@
<Window x:Class="MVVMwithWPF.Views.ConverterWindoew"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:MVVMwithWPF.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MVVMwithWPF.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:MVVMwithWPF.ViewModels"
Title="ConverterWindoew"
Width="800"
Height="450"
mc:Ignorable="d">
<Window.DataContext>
<viewModels:ConverterViewModel />
</Window.DataContext>
<Window.Resources>
<converters:InvertedBooleanConverter x:Key="ReverseBConverter" />
<converters:BoolToStringConverter x:Key="B2SConverter" />
<converters:BoolToVisibilityConverter x:Key="B2VConverter" />
<converters:BoolToVisibilityConverter x:Key="ReverseB2VConverter"
FalseValue="Visible" TrueVlaue="Collapsed" />
<converters:ParameterContainValueConverter x:Key="ParamContainConverter" />
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<DataGrid AutoGenerateColumns="False" CanUserAddRows="False" IsReadOnly="True"
ItemsSource="{Binding Person}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}" Header="Id" />
<DataGridTextColumn Binding="{Binding Name}" Header="이름" />
<DataGridTextColumn Binding="{Binding Sex}" Header="성별" />
<DataGridTextColumn Binding="{Binding DepartmentCode}" Header="부서" />
</DataGrid.Columns>
</DataGrid>
<StackPanel Grid.Column="1" Margin="5">
<CheckBox Content="IsEdit" IsChecked="{Binding IsEdit, Mode=TwoWay}" />
<TextBlock Text="IsEnable Binding" />
<TextBox IsEnabled="{Binding IsEdit}" />
<TextBlock Text="IsReadOnly Reverse Binding" />
<TextBox IsReadOnly="{Binding IsEdit, Converter={StaticResource ReverseBConverter}}" />
<TextBlock Text="IsEdit Visibility" Visibility="{Binding IsEdit, Converter={StaticResource B2VConverter}}" />
<TextBlock Text="IsEdit Reverse Visibility" Visibility="{Binding IsEdit, Converter={StaticResource ReverseB2VConverter}}" />
<TextBlock Text="Parameter Contain Value Test" />
<TextBox x:Name="tbxParam" />
<CheckBox IsEnabled="False" Content="apple|banana|strawberry" IsChecked="{Binding ElementName=tbxParam, Path=Text, Converter={StaticResource ParamContainConverter}, ConverterParameter=apple|banana|strawberry}" />
</StackPanel>
</Grid>
</Window>

@ -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
{
/// <summary>
/// ConverterWindoew.xaml에 대한 상호 작용 논리
/// </summary>
public partial class ConverterWindoew : Window
{
public ConverterWindoew()
{
InitializeComponent();
}
}
}
Loading…
Cancel
Save