【问题标题】:how to access Attached property in MultiBuinding of Textbox using RelativeSource如何使用RelativeSource访问文本框MultiBuinding中的附加属性
【发布时间】:2017-05-11 14:48:21
【问题描述】:

我正在通过 this 教程学习 WPF,我坚持使用 lecture 20。我遵循了每一步但我的代码给出了错误

不支持嵌套类型: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 类是

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

第二次绑定路径时出现错误

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

谁能告诉我如何在上述情况下在路径中引用 Canvas.Top

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

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

但我想通过使用 Canvas.Top 来解决我的问题。

谢谢。

【问题讨论】:

    标签: c# .net wpf multibinding


    【解决方案1】:

    而不是这个,

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

    这样使用,

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

    大括号被替换。

    【讨论】:

    • 谢谢。有效。我已经尝试过不同的组合,但我忘记了这个。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 2017-07-25
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    相关资源
    最近更新 更多