binding validation & converter
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user