无法解决这个问题,我试图让这个模板尽可能多样化。尝试居中TextBlock时,我收到以下错误,HorizontalAlignment不起作用。
System.Windows.Data错误:5:BindingExpression产生的值是 对目标财产无效。值=' 0' BindingExpression:路径= ActualWidth的;的DataItem ='网格' (名称=' PathGrid&#39); 目标元素是' TextBlock' (名称=' PathPercentage&#39);目标财产 是' FontSize' (键入' Double')
System.Windows.Data错误:5:BindingExpression产生的值是 对目标财产无效。值=' -6 -6 -6 -6' MultiBindingExpression:目标元素是' TextBlock' (名称=' PathPercentage&#39);目标财产是'保证金' (类型 '厚度&#39)
System.Windows.Data错误:5:BindingExpression产生的值是 对目标财产无效。价值=' -17 -17 -17 -17' MultiBindingExpression:目标元素是' TextBlock' (名称=' PathPercentage&#39);目标财产是'保证金' (类型 '厚度&#39)
System.Windows.Data错误:5:BindingExpression产生的值是 对目标财产无效。价值=' 83 83 83 83' MultiBindingExpression:目标元素是' TextBlock' (名称=' PathPercentage&#39);目标财产是'保证金' (类型 '厚度&#39)
这是带有我的绑定的ControlTemplate
<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}" />
这是我的转换器,它应该产生以我的文本块为中心的边距值:
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();
}
}
}
答案 0 :(得分:2)
Woops,设法修复它。只需确保它返回类型Thickness
。我假设它应该返回Margin
作为一种类型,这就是我努力找到它的原因!
double t = Math.Round(((parentWidth / 2) - (fontSize / 2)), 0);
Thickness margin = new Thickness(t, t, t, t);
return margin;