converter sample
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace ValueConverterSample.Converter
|
||||
{
|
||||
internal class YesNoToBoolConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
if (value == null)
|
||||
return false;
|
||||
|
||||
switch (value.ToString().ToUpper())
|
||||
{
|
||||
case "YES":
|
||||
return true;
|
||||
case "NO":
|
||||
return false;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
if (!(value is bool))
|
||||
return "NO";
|
||||
|
||||
bool converted = (bool)value;
|
||||
if (converted)
|
||||
return "YES";
|
||||
else
|
||||
return "NO";
|
||||
}
|
||||
}
|
||||
}
|
@@ -4,9 +4,23 @@
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:ValueConverterSample"
|
||||
xmlns:cvt="clr-namespace:ValueConverterSample.Converter"
|
||||
mc:Ignorable="d"
|
||||
Title="MainWindow" Height="450" Width="800">
|
||||
<Grid>
|
||||
|
||||
</Grid>
|
||||
Title="MainWindow" Height="100" Width="300">
|
||||
<Window.Resources>
|
||||
<cvt:YesNoToBoolConverter x:Key="ynConvert"/>
|
||||
</Window.Resources>
|
||||
|
||||
<StackPanel>
|
||||
<TextBox x:Name="tbMain"
|
||||
Text="YES"/>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<TextBlock Text="Current value: "/>
|
||||
<TextBlock x:Name="tbCheck"
|
||||
Text="{Binding ElementName=tbMain, Path=Text, Converter={StaticResource ynConvert}}"/>
|
||||
</StackPanel>
|
||||
|
||||
<CheckBox Content="{Binding ElementName=tbMain, Path=Text}"
|
||||
IsChecked="{Binding ElementName=tbMain, Path=Text, Converter={StaticResource ynConvert}}"/>
|
||||
</StackPanel>
|
||||
</Window>
|
||||
|
@@ -62,6 +62,7 @@
|
||||
<DependentUpon>App.xaml</DependentUpon>
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Converter\YesNoToBoolConverter.cs" />
|
||||
<Compile Include="MainWindow.xaml.cs">
|
||||
<DependentUpon>MainWindow.xaml</DependentUpon>
|
||||
<SubType>Code</SubType>
|
||||
|
Reference in New Issue
Block a user