Can't figure this out, i'm trying to make this Template as versatile as possible. I'm getting the following errors when attempting to centre a TextBlock, HorizontalAlignment doesn't work.
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='0' BindingExpression:Path=ActualWidth; DataItem='Grid' (Name='PathGrid'); target element is 'TextBlock' (Name='PathPercentage'); target property is 'FontSize' (type 'Double')
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='-6 -6 -6 -6' MultiBindingExpression:target element is 'TextBlock' (Name='PathPercentage'); target property is 'Margin' (type 'Thickness')
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='-17 -17 -17 -17' MultiBindingExpression:target element is 'TextBlock' (Name='PathPercentage'); target property is 'Margin' (type 'Thickness')
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='83 83 83 83' MultiBindingExpression:target element is 'TextBlock' (Name='PathPercentage'); target property is 'Margin' (type 'Thickness')
This is the ControlTemplate with my bindings
<Grid x:Name="PathGrid" Margin="2" Width="200">
<Canvas>
<TextBlock x:Name="PathPercentage" Text="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Value}"
Foreground="White"
FontSize="{Binding ElementName=PathGrid, Path=ActualWidth, Converter={StaticResource SizeTextOnParent}}">
<TextBlock.Margin>
<MultiBinding Converter="{StaticResource CenterElement}">
<Binding ElementName="PathGrid" Path="ActualWidth"/>
<Binding ElementName="PathPercentage" Path="FontSize"/>
</MultiBinding>
</TextBlock.Margin>
</TextBlock>
<TextBlock Text="{Binding ElementName=PathPercentage, Path=Margin}" />
<Ellipse Fill="Transparent"
Stroke="#434953"
StrokeThickness="3"
Width="{Binding ElementName=PathGrid, Path=ActualWidth}"
Height="{Binding ElementName=PathGrid, Path=ActualWidth}" />
And here is my converter which should produce margin values which will centre my textblock:
using System;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;
namespace Test_Project.Converters
{
public class CenterElement : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
double parentWidth = (double)values[0];
double fontSize = (double)values[1];
double t = Math.Round(((parentWidth / 2) - (fontSize / 2)), 0);
string margin = t + " " + t + " " + t + " " + t;
return margin;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}