I'm trying to bind an integer to a ControlTemplate but shows nothing to me.
here is my TextBlock which is a part of my ControlTemplate:
<TextBlock FontSize="14"
Foreground="#000"
Text="{TemplateBinding Number}"/>
here is my dependency property which is a part of my CustomControl:
public static readonly DependencyProperty NumberProperty = DependencyProperty.Register(nameof(Number), typeof(int), typeof(MyCustomControl), new PropertyMetadata(0));
public int Number
{
get => (int)GetValue(NumberProperty);
set => SetValue(NumberProperty, value);
}
I have tried this also, but it doesn't works too:
<TextBlock FontSize="14"
Foreground="#000"
Text="{Binding Number, RelativeSource={RelativeSource TemplatedParent}, TargetNullValue=''}"/>
if I change it to string it works
You have to use a converter which will convert int to string value. Here's the example of such converter.
Instead of the
TemplatedBindingin the control template you should use such statement.One more thing. You have to define the converter somewhere in your xaml to be able to use it as a
StaticResource.Update
Converter is not needed to show integer value as text.