From de76ae38b43a97b4a613701292604aac67b24446 Mon Sep 17 00:00:00 2001 From: syneffort Date: Tue, 4 Jul 2023 15:07:28 +0900 Subject: [PATCH] binding validation & converter --- .../Converter/ButtonTextBrush.cs | 43 +++++++++++++++++++ .../MainWindow.xaml | 7 ++- PacticeSolution/PacticeSolution.sln | 8 ++-- 3 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 PacticeSolution/BindingValidationRuleSample/Converter/ButtonTextBrush.cs diff --git a/PacticeSolution/BindingValidationRuleSample/Converter/ButtonTextBrush.cs b/PacticeSolution/BindingValidationRuleSample/Converter/ButtonTextBrush.cs new file mode 100644 index 0000000..a8d6824 --- /dev/null +++ b/PacticeSolution/BindingValidationRuleSample/Converter/ButtonTextBrush.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Data; +using System.Windows.Media; + +namespace BindingValidationRuleSample.Converter +{ + [ValueConversion(typeof(int), typeof(Brush))] + internal class ButtonTextBrush : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (targetType != typeof(Brush)) + return null; + + if (value == null) + return null; + + int age = int.Parse(value.ToString()); + return ValueColor(age); + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + return null; + } + + private Brush ValueColor(int age) + { + if (age >= 10 && age < 20) + return Brushes.HotPink; + + if (age >= 20 && age < 30) + return Brushes.DarkOliveGreen; + + return Brushes.MediumPurple; + } + } +} diff --git a/PacticeSolution/BindingValidationRuleSample/MainWindow.xaml b/PacticeSolution/BindingValidationRuleSample/MainWindow.xaml index 9e6e75a..fe97c39 100644 --- a/PacticeSolution/BindingValidationRuleSample/MainWindow.xaml +++ b/PacticeSolution/BindingValidationRuleSample/MainWindow.xaml @@ -6,11 +6,15 @@ xmlns:local="clr-namespace:BindingValidationRuleSample" xmlns:model="clr-namespace:BindingValidationRuleSample.Model" xmlns:validation="clr-namespace:BindingValidationRuleSample.Validation" + xmlns:converter="clr-namespace:BindingValidationRuleSample.Converter" mc:Ignorable="d" Title="MainWindow" Height="200" Width="300"> + + + @@ -45,7 +49,8 @@ - diff --git a/PacticeSolution/PacticeSolution.sln b/PacticeSolution/PacticeSolution.sln index 8e24b32..2fc5041 100644 --- a/PacticeSolution/PacticeSolution.sln +++ b/PacticeSolution/PacticeSolution.sln @@ -47,13 +47,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LabelTextBlockTextBoxSample EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ListBoxBindingSample", "ListBoxBindingSample\ListBoxBindingSample.csproj", "{71B7E9C4-C73B-4701-B702-A91AB5630580}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KeyboardInputSample", "KeyboardInputSample\KeyboardInputSample.csproj", "{B6A7E0D5-A2C7-4083-A060-1672E6FBDC82}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KeyboardInputSample", "KeyboardInputSample\KeyboardInputSample.csproj", "{B6A7E0D5-A2C7-4083-A060-1672E6FBDC82}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TabControlSample", "TabControlSample\TabControlSample.csproj", "{7A148D37-36D1-43B5-A133-B02CE5CB743F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TabControlSample", "TabControlSample\TabControlSample.csproj", "{7A148D37-36D1-43B5-A133-B02CE5CB743F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProgressBarSample", "ProgressBarSample\ProgressBarSample.csproj", "{EB304FB7-67EA-467E-9BE4-E55187415CAB}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProgressBarSample", "ProgressBarSample\ProgressBarSample.csproj", "{EB304FB7-67EA-467E-9BE4-E55187415CAB}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BindingValidationRuleSample", "BindingValidationRuleSample\BindingValidationRuleSample.csproj", "{B328C31A-8C85-4A48-A12F-26E11C37A15C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BindingValidationRuleSample", "BindingValidationRuleSample\BindingValidationRuleSample.csproj", "{B328C31A-8C85-4A48-A12F-26E11C37A15C}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution