gpt4 book ai didi

c# - WPF:BindingExpression 产生的值对目标无效

转载 作者:太空宇宙 更新时间:2023-11-03 10:21:12 27 4
gpt4 key购买 nike

无法解决这个问题,我正在努力使这个模板尽可能多才多艺。尝试将 TextBlock 居中时出现以下错误,Horizo​​ntalAlignment 不起作用。

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')

这是带有我的绑定(bind)的 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}" />

这是我的转换器,它应该产生将我的文本 block 居中的边距值:

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();
}
}
}

最佳答案

哎呀,设法修复它。只需确保它返回类型 Thickness。我假设它应该返回 Margin 作为一种类型,这就是我努力寻找它的原因!

double t = Math.Round(((parentWidth / 2) - (fontSize / 2)), 0);
Thickness margin = new Thickness(t, t, t, t);

return margin;

关于c# - WPF:BindingExpression 产生的值对目标无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33467865/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com