binding validation & converter

main
syneffort 2 years ago
parent 5e4f2e6fdc
commit de76ae38b4
  1. 43
      PacticeSolution/BindingValidationRuleSample/Converter/ButtonTextBrush.cs
  2. 7
      PacticeSolution/BindingValidationRuleSample/MainWindow.xaml
  3. 8
      PacticeSolution/PacticeSolution.sln

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

@ -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">
<Window.DataContext>
<model:Student/>
</Window.DataContext>
<Window.Resources>
<converter:ButtonTextBrush x:Key="cvtAgeToBrush"/>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
@ -45,7 +49,8 @@
</Binding>
</TextBox.Text>
</TextBox>
<TextBox Grid.Column="1" Grid.Row="2" Margin="5" Foreground="Blue"
<TextBox Grid.Column="1" Grid.Row="2" Margin="5"
Foreground="{Binding Path=Age, Mode=TwoWay, Converter={StaticResource cvtAgeToBrush}}"
Text="{Binding PhoneNumber, Mode=TwoWay}"/>
<Grid Grid.Column="0" Grid.Row="4" Grid.ColumnSpan="2" Height="30">

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

Loading…
Cancel
Save