如何使用 RelativeSource 在 Textbox 的 MultiBuinding 中访问 Attached 属性

how to access Attached property in MultiBuinding of Textbox using RelativeSource

我正在通过 this tutorials and I stuck on lecture 20 学习 WPF。我遵循了每一步但是我的代码给出了错误

Nested types are not supported: Canvas.Top

重点是,在讲座视频中运行成功

我的XAML代码是:

<Window.Resources>
    <local:ThresholdRuleConverter x:Key="ruleConverter" />
</Window.Resources>
<StackPanel>

    <TextBox x:Name="txtAmount" Text="{Binding Path=ItemAmount}"
             HorizontalAlignment="Stretch"
             Tag="{Binding Path=ItemAmount, Mode=OneTime}" Height="35" FontSize="22"
             Canvas.Top="{Binding Path=Threshold}">
        <TextBox.Background>
            <MultiBinding Converter="{StaticResource ruleConverter}" ConverterParameter="Red,Yellow,Green">
                <Binding Mode="OneWay" RelativeSource="{RelativeSource Self}" Path="Tag" />
                <Binding Mode="OneWay"  RelativeSource="{RelativeSource Self}" Path="{Canvas.Top}" />
                <Binding Mode="OneWay" RelativeSource="{RelativeSource Self}" Path="Text" />
            </MultiBinding>
        </TextBox.Background>
    </TextBox>

</StackPanel>

而我的 ThresholdRuleConverter Class 是

public class ThresholdRuleConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        //define base colors
        SolidColorBrush invalidBrush = new SolidColorBrush(Colors.Red);
        SolidColorBrush equalBrush = new SolidColorBrush(Colors.Yellow);
        SolidColorBrush validBrush = new SolidColorBrush(Colors.Green);

        if (parameter != null)
        {
            string[] definedColors = parameter.ToString().Split(',');
            BrushConverter converter = new BrushConverter();
            if (definedColors.Length > 0)
            {
                invalidBrush = converter.ConvertFromString(definedColors[0]) as SolidColorBrush;
                if (definedColors.Length > 1)
                    equalBrush = converter.ConvertFromString(definedColors[1]) as SolidColorBrush;
                if (definedColors.Length > 2)
                    validBrush = converter.ConvertFromString(definedColors[2]) as SolidColorBrush;
            }
        }
        if (values.Length < 3)
            return invalidBrush;

        try
        {
            if (values[0] != DependencyProperty.UnsetValue && values[1] != DependencyProperty.UnsetValue
                && values[2] != DependencyProperty.UnsetValue)
            {
                int oldValue = System.Convert.ToInt32(values[0]);
                int thresholdValue = System.Convert.ToInt32(values[1]);
                int newValue = System.Convert.ToInt32(values[2]);

                if (newValue > oldValue && (newValue - oldValue) <= thresholdValue)
                    return validBrush;
                else if (newValue == oldValue)
                    return equalBrush;
                else
                    return invalidBrush;
            }
            return invalidBrush;
        }
        catch (Exception)
        {
            return invalidBrush;
        }
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

我的简单 txtAmount DataContext 是

txtAmount.DataContext = new Widget { ItemAmount = 25, Threshold = 50 };

错误发生在 Path 中的第二次绑定

<Binding Mode="OneWay"  RelativeSource="{RelativeSource Self}" Path="{Canvas.Top}" />

任何人都可以告诉我如何在上述场景中的路径中引用 Canvas.Top

一种解决方法是我可以直接使用这个:

<Binding Mode="OneWay" Path="Threshold" />

但是我想用Canvas.Top来解决我的问题。

谢谢。

而不是这个,

<Binding Mode="OneWay"  RelativeSource="{RelativeSource Self}" Path="{Canvas.Top}" />

这样使用,

<Binding Mode="OneWay"  RelativeSource="{RelativeSource Self}" Path="(Canvas.Top)" />

花括号被替换。