binding validation & converter

This commit is contained in:
2023-07-04 15:07:28 +09:00
parent 5e4f2e6fdc
commit de76ae38b4
3 changed files with 53 additions and 5 deletions

View File

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