TemplateBindings in user controls

I'm just fooling myself with custom controls in silverlight and for life I can't get TemplateBindings to work. Can someone give this smaller version once to see that I missed something.

So my ControlTemplate in generic.xaml looks like

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:NumericStepperControl;assembly=NumericStepperControl"> <Style TargetType="local:NumericStepper"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="local:NumericStepper"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Border Grid.Column="0" BorderBrush="Black" BorderThickness="2" Width="50" Height="30"> <TextBlock Width="50" Height="30" Text="{TemplateBinding Value}" /> </Border> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary> 

and my management class looks like this:

 namespace NumericStepperControl { public class NumericStepper : Control { public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(int), typeof(NumericStepper), new PropertyMetadata(20)); public NumericStepper() : base() { DefaultStyleKey = typeof( NumericStepper ); } public int Value { get { return (int)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } } } } 

I expect that when this starts, TextBlock will show number 20. Any ideas as to why this is not working?

As a side, I don’t have a separate project that contains a reference to the NumericStepperControl assembly, and when it launches the controls, it seems that they are correctly built.

Edit ... after a little investigation, I found that if I change the type of the Value property to a string that works fine. Why doesn't a text block just call toString on everything that is passed into it? Is there any way around this, as I see it happening a lot?

+4
source share
2 answers

After a little digging, it turns out that TextBlock does not actually call ToString on everything that is transmitted. To get around this, you should use a converter to call ToString for you.

Here, however, TemplateBinding does not support Converters. You must add a TemplateBinding to the DataContext, and then use the regular binding in the Text property along with the converter.

So the markup of the TextBlock becomes

  <TextBlock Width="50" Height="30" DataContext="{TemplateBinding Value}" Text="{Binding Converter={StaticResource NumberTypeToStringConverter}}" /> 

My custom converter:

 public class NumberTypeToStringConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null) { throw new NullReferenceException(); } return value.ToString(); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { MethodInfo methodInfo = targetType.GetMethod("Parse"); if (methodInfo == null) { throw new MissingMethodException("The targetType to convert back to a Number must implement a Parse method"); } return methodInfo.Invoke(null, new object[] { value }); } } 

It seems like a little cool, and I would be interested to hear if it has any negative consequences. Also, if someone is reading this and something is wrong with my converter, please let me know.

Greetings

+11
source

There are various approaches to solving the problem. Found this description by Marek Latukevich .

0
source

All Articles