0

Hy guys, I need your help again.

My XAML:

I would like to bind the Color of Line1 to my TestClassItem in XAML.

<Canvas Name="ElementCanvas">
    <local:TestClass x:Name="TestClassItem" Width="50" Height="20" Position="20,100" LineColor="{Binding Stroke, ElementName=Line1}" ></local:TestClass>
    <Line x:Name="Line1" X1="100" X2="300" Y1="50" Y2="50" Stroke="Red"/>
    <Line x:Name="Line2" X1="100" X2="300" Y1="100" Y2="100" Stroke="{Binding Stroke, ElementName=Line1}" />
</Canvas>

My Created Class is pretty simple:

public class TestClass : Canvas
{
    Line myLine = new Line();
    public Color LineColor
    {
        get
        {
            return (Color)GetValue(LineStrokeProperty);
        }
        set
        {
            SetValue(LineStrokeProperty, value);
            OnPropertyChanged("LineColor");
        }
    }

    double X
    {
        get
        {
            return (double)Canvas.GetLeft(this);
        }
        set
        {
            Canvas.SetLeft(this, value);
            OnPropertyChanged("X");
        }
    }

    double Y
    {
        get
        {
            return (double)Canvas.GetTop(this) + Height / 2;
        }
        set
        {
            Canvas.SetTop(this, value - Height / 2);
            OnPropertyChanged("Y");
        }
    }

    public Point Position
    {
        get
        {
            return new Point(X, Y);
        }
        set
        {
            X = value.X;
            Y = value.Y;
        }
    }

    public static readonly DependencyProperty LineStrokeProperty = DependencyProperty.Register("LineColor", typeof(Color), typeof(TestClass), new FrameworkPropertyMetadata(Colors.Purple, FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(OnLineStrokePropertyChanged), new CoerceValueCallback(OnLineStrokePropertyCoerceValue)), new ValidateValueCallback(OnLineStrokePropertyValidateValue));

    private static bool OnLineStrokePropertyValidateValue(object value)
    {
        return true;
    }

    private static object OnLineStrokePropertyCoerceValue(DependencyObject d, object baseValue)
    {
        return (Color)baseValue;
    }

    private static void OnLineStrokePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((TestClass)d).SetValue(LineStrokeProperty, (Color)e.NewValue);

    }




    public TestClass()
    {
        DataContext = this;
        Background = Brushes.Yellow;
        SnapsToDevicePixels = true;
        Binding b_width = new Binding("Width");
        b_width.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        myLine.SetBinding(Line.X2Property, b_width);

        Binding b_y1 = new Binding("Height");
        b_y1.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        b_y1.Converter = new DoubleTwicer(); //Divides Height by 2
        myLine.SetBinding(Line.Y1Property, b_y1);

        Binding b_y2 = new Binding("Height");
        b_y2.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        b_y2.Converter = new DoubleTwicer();
        myLine.SetBinding(Line.Y2Property, b_y2);

        Binding b_stroke = new Binding("LineColor");
        b_stroke.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        b_stroke.Converter = new RaColorToSolidBrush();
        myLine.SetBinding(Line.StrokeProperty, b_stroke);

        this.Children.Add(myLine);
    }

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

}

Why doesn't it work?

Does anybody know how to Debug (see Values of bindings in Visual Studio?)

Thank you sou much!!!!

daniele3004
  • 13,072
  • 12
  • 67
  • 75
MisterPresident
  • 563
  • 7
  • 20
  • http://stackoverflow.com/questions/12404337/binding-between-my-usercontrol-and-viewmodel check my answer from here and see the comments. DataContext=this; is not that good when you wanna use a usercontrol with Dependency Properties – blindmeis Oct 13 '14 at 11:58

1 Answers1

0

First of all! Thank you guys you already helped me but it works but not completely (DataContext = this; might be the problem so leave it:

 public Brush LineColor
        {
            get
            {
                return (Brush)GetValue(LineStrokeProperty);
            }
            set
            {
                SetValue(LineStrokeProperty, value);
                OnPropertyChanged("LineColor");
            }
        }



        public static readonly DependencyProperty LineStrokeProperty = DependencyProperty.Register("LineColor", typeof(Brush), typeof(RaClickableLine), new FrameworkPropertyMetadata(Brushes.White, FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(OnLineStrokePropertyChanged), new CoerceValueCallback(OnLineStrokePropertyCoerceValue)), new ValidateValueCallback(OnLineStrokePropertyValidateValue));

        private static bool OnLineStrokePropertyValidateValue(object value)
        {
            return true;
        }

        private static object OnLineStrokePropertyCoerceValue(DependencyObject d, object baseValue)
        {
            return (Brush)baseValue;
        }

        private static void OnLineStrokePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((RaClickableLine)d).SetValue(LineStrokeProperty, (Brush)e.NewValue);

        }


public RaClickableLine()
        {
            SetValue(FrameworkElement.NameProperty, "ClickableLine");
            Background = Brushes.AliceBlue;
            SnapsToDevicePixels = true;

            myLine.Stroke = Brushes.Blue;
            myLine.X1 = 0;
            myLine.X2 = ActualWidth;
            myLine.Y1 = 10;
            myLine.Y2 = 10;


            Binding b_width = new Binding();
            b_width.ElementName = "ClickableLine";
            b_width.Path = new PropertyPath("Width");
            b_width.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            myLine.SetBinding(Line.X2Property, b_width);

            Children.Add(myLine);
        }

It even don't show me the line. If I leave b_width.ElementName = "ClickableLine"; and add DataContext = this; the line appears.

MisterPresident
  • 563
  • 7
  • 20