1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
public class SampleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string str = value as string;
if (String.IsNullOrEmpty(str))
{
return 0;
}
else
{
int n = 0;
for (int i = 0; i < str.Length; i++)
{
n +=System.Convert.ToInt32(str[i]);
}
return n;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
2/le Class ValueConverterControl avec son nécessaire attribut TemplatePart
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
[TemplatePart(Name = "PART_tb", Type = typeof(TextBlock))]
public class ValueConverterControl : Control
{
private TextBlock currentTb;
public ValueConverterControl()
{
}
static ValueConverterControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ValueConverterControl),
new FrameworkPropertyMetadata(typeof(ValueConverterControl)));
}
public IValueConverter ValueConverter
{
get { return (IValueConverter)GetValue(ValueConverterProperty); }
set { SetValue(ValueConverterProperty, value); }
}
// Using a DependencyProperty as the backing store for ValueConverter. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ValueConverterProperty =
DependencyProperty.Register("ValueConverter",
typeof(IValueConverter),
typeof(ValueConverterControl),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
public string TheText
{
get { return (string)GetValue(TheTextProperty); }
set { SetValue(TheTextProperty, value); }
}
// Using a DependencyProperty as the backing store for TheText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TheTextProperty =
DependencyProperty.Register("TheText",
typeof(string),
typeof(ValueConverterControl),
new FrameworkPropertyMetadata(string.Empty,
FrameworkPropertyMetadataOptions.AffectsRender,
OnTheTextChanged));
private static void OnTheTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
{
ValueConverterControl ctl = d as ValueConverterControl;
string txt = (string)args.NewValue;
if (ctl != null)
{
ctl.InvalidateVisual();
}
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if (this.Template != null )
{
TextBlock tb = this.Template.FindName("PART_tb", this) as TextBlock;
if (tb !=null)
{
currentTb = tb;
if (this.TheText !=null )
{
Binding myBinding = new Binding();
myBinding.Path =new PropertyPath( "TheText");
myBinding.Converter = this.ValueConverter;
myBinding.Source = this;
currentTb.SetBinding(TextBlock.TextProperty, myBinding);
}
}
}
}
} |
2/ son Generic.xaml |
<Style TargetType="{x:Type local:ValueConverterControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ValueConverterControl}">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<TextBlock
x:Name="PART_tb"
Width="{TemplateBinding local:ValueConverterControl.Width}"
Height="{TemplateBinding local:ValueConverterControl.Width}"
Foreground="{TemplateBinding local:ValueConverterControl.Foreground}"
FontSize="{TemplateBinding local:ValueConverterControl.FontSize}"
TextAlignment="Left"
Text="MOMO">
</TextBlock>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style> |
Partager