简体   繁体   English

UserControl DataBinding不起作用

[英]UserControl DataBinding not working

Hy guys, I need your help again. 大家好,我再次需要您的帮助。

My XAML: 我的XAML:

I would like to bind the Color of Line1 to my TestClassItem in XAML. 我想将Line1的颜色绑定到XAML中的TestClassItem。

<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?) 是否有人知道如何调试(请参阅Visual Studio中的绑定值?)

Thank you sou much!!!! 非常感谢你!!!

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: 谢谢大家,您已经帮助过我,但是它可以正常运行,但效果不完全(DataContext = this;可能是问题所在,所以请保留它:

 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"; 如果我离开b_width.ElementName = "ClickableLine"; and add DataContext = this; 并添加DataContext = this; the line appears. 该行出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM