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

标签 c# wpf

无法解决这个问题,我正在努力使这个模板尽可能多才多艺。尝试将 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/

相关文章:

c# - WPF MVVM 事件订阅

wpf - ComboBox 在底部有额外的空间

c# - 每个人都在 Windows 8 中

c# - 如何从 ServiceStack 元数据插件中删除与身份验证相关的路由?

c# - 拖放到桌面/资源管理器

c# - ResourceDictionary : {"Key cannot be null.\r\nParameter name: key"} 中的运行时错误

wpf - 在 Wpf 中创建垂直菜单

c# - 如何使用MimeKit获取电子邮件的所见即所得主体

c# - 使用 MVC SimpleMembership 分配角色

c# - 我是否正确回答了这个面试挑战? (在 C# 中检查有效的二叉树)