<Window x:Class="WindowsApplication4.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:system="clr-namespace:System" xmlns:drawing="clr-namespace:System.Drawing;assembly=System.Drawing" xmlns:local="clr-namespace:WindowsApplication4" Title="CLR Property As Binding Target Sample" Height="300" Width="300"> <Window.Resources> <ObjectDataProvider ObjectType="{x:Type drawing:Color}" MethodName="FromArgb" x:Key="WorkingColor"> <ObjectDataProvider.MethodParameters> <system:Int32>255</system:Int32> <system:Int32>255</system:Int32> <system:Int32>255</system:Int32> <system:Int32>255</system:Int32> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> <ObjectDataProvider ObjectInstance="{StaticResource WorkingColor}" MethodName="GetBrightness" x:Key="ColorBrightness"> </ObjectDataProvider> <ObjectDataProvider ObjectInstance="{StaticResource WorkingColor}" MethodName="GetHue" x:Key="ColorHue"> </ObjectDataProvider> <ObjectDataProvider ObjectInstance="{StaticResource WorkingColor}" MethodName="GetSaturation" x:Key="ColorSaturation"> </ObjectDataProvider> <Style x:Key="LabelStyle" TargetType="{x:Type TextBlock}"> <Setter Property="Margin" Value="5,3,0,0"/> </Style> <local:IntToString x:Key="IntToString" /> </Window.Resources> <StackPanel> <StackPanel Orientation="Horizontal"> <TextBlock Text="R: " Style="{StaticResource LabelStyle}"/> <TextBox Width="50" Name="RPart" Text="{Binding Source={StaticResource WorkingColor}, Path=MethodParameters1, BindsDirectlyToSource=True, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource IntToString}}"/> <TextBlock Text="G: " Style="{StaticResource LabelStyle}"/> <TextBox Width="50" Text="{Binding Source={StaticResource WorkingColor}, Path=MethodParameters2, BindsDirectlyToSource=True, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource IntToString}}"/> <TextBlock Text="B: " Style="{StaticResource LabelStyle}"/> <TextBox Width="50" Text="{Binding Source={StaticResource WorkingColor}, Path=MethodParameters3, BindsDirectlyToSource=True, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource IntToString}}"/> </StackPanel> <StackPanel Orientation="Horizontal"> <TextBlock Text="Brightness: "/> <TextBlock Text="{Binding Source={StaticResource ColorBrightness}}"/> </StackPanel> <StackPanel Orientation="Horizontal"> <TextBlock Text="Hue: "/> <TextBlock Text="{Binding Source={StaticResource ColorHue}}"/> </StackPanel> <StackPanel Orientation="Horizontal"> <TextBlock Text="Saturation: "/> <TextBlock Text="{Binding Source={StaticResource ColorSaturation}}"/> </StackPanel> </StackPanel> </Window>
public class IntToString : IValueConverter { public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture ) { if( value != null ) { return value.ToString(); } return null; } public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture ) { string strValue = value as string; if( strValue != null ) { int result; bool converted = int.TryParse( strValue, out result ); if( converted ) { return result; } } return null; } }