how to access Attached property in MultiBuinding of Textbox using RelativeSource

151 views Asked by At

I am learning WPF through this tutorials and I stuck on lecture 20. I followed every step But my code gives error

Nested types are not supported: Canvas.Top

The point is, it is running successfully in the lecture Video

My XAML code is:

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

Whereas my ThresholdRuleConverter Class is

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

and my simple txtAmount DataContext is

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

The error is occured on second binding in Path

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

Can anyone please tell me how can I reference Canvas.Top in path in above scenario.

One way to Solve is that I can directly use this:

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

But I want to solve my problem by using Canvas.Top.

Thanks.

1

There are 1 answers

1
WPFUser On BEST ANSWER

instead of this,

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

use like this,

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

curly braces replaced.