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?
source share