【问题标题】:How Do I Bind Properties in Code Behind WPF如何在 WPF 后面的代码中绑定属性
【发布时间】:2020-05-05 04:13:56
【问题描述】:

我对 WPF 很陌生...所以我需要将线 X1 和 Y1 属性与椭圆 canvas.left 和 canvas.top 属性绑定......

在 XAML 中它可以正常工作...

XAML 代码

<Ellipse x:Name="tst" Width="40" Height="40" Canvas.Left="150" Canvas.Top="150" Stroke="Blue" StrokeThickness="2" MouseMove="adjustRoute"/>
<Line X1="{Binding ElementName=tst, Path=(Canvas.Left)}" Y1="{Binding ElementName=tst, Path=(Canvas.Top)}" X2="300" Y2="200" Stroke="Blue" StrokeThickness="2"/>

但我需要在使用 C# 的代码中执行此操作

所以我这样做了

temp = new Line();
tempe = new Ellipse();
tempe.Fill = Brushes.Transparent;
tempe.Stroke = Brushes.Blue;
tempe.StrokeThickness = 1;
tempe.Width = 20;
tempe.Height = 20;
Canvas.SetLeft(tempe, currentPoint.X-10);
Canvas.SetTop(tempe, currentPoint.Y-10);
tempe.MouseMove += adjustRoute;
Binding binding = new Binding { Source = tempe, Path = new PropertyPath(Canvas.LeftProperty), UpdateSourceTrigger=UpdateSourceTrigger.PropertyChanged };
temp.SetBinding(Line.X1Property, binding);
Binding binding2 = new Binding { Source = tempe, Path = new PropertyPath(Canvas.TopProperty), UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged };
temp.SetBinding(Line.Y1Property, binding2);
temp.Stroke = Brushes.Blue;
temp.StrokeThickness = 2;
temp.X1 = currentPoint.X;
temp.Y1 = currentPoint.Y;
temp.X2 = currentPoint.X + 200;
temp.Y2 = currentPoint.Y + 200;
testcanv.Children.Add(temp);
testcanv.Children.Add(tempe);

但是当我移动椭圆时它不会更新行位置(在 XAML 中它会更新)......

这里 currentPoint 是我在运行时通过鼠标单击来绘制形状的点,而 adjustRoute 是在拖动时移动它们的函数

我在这里做错了什么?

谢谢

【问题讨论】:

  • 我猜你将不得不处理绑定中的UpdateSourceTrigger。您可以通过将要绑定到的dependencyProperty 的UpdateSourceTrigger 设置为OnPropertyChanged 来做到这一点,然后在您的代码隐藏中实现INotifyPropertyChanged 接口。
  • 如果你想明确地处理更新,这里有一个链接:wpf-tutorial.com/data-binding/…
  • 这里是 INotifyPropertyChange 接口的解决方案:stackoverflow.com/a/1316417/7274172
  • @Luchspeter 这与 UpdateSourceTrigger 无关,仅与 TwoWay 绑定有关。 INotifyPropertyChange 也无关紧要,因为源属性是依赖属性。
  • @Clemens 哦,好吧-感谢您的提示。但是您不应该能够将属性绑定到Canvas.Left 并像这样处理更新吗?

标签: c# wpf data-binding


【解决方案1】:

确保只创建一次 Line 和 Ellipse 元素,并且只分配一次绑定。将 MouseMove 处理程序分配给 Canvas 而不是 Ellipse:

private Line line;
private Ellipse ellipse;

public MainWindow()
{
    InitializeComponent();

    line = new Line
    {
        Stroke = Brushes.Blue,
        StrokeThickness = 2
    };

    ellipse = new Ellipse
    {
        Stroke = Brushes.Blue,
        StrokeThickness = 1,
        Width = 20,
        Height = 20,
        Margin = new Thickness(-10)
    };

    Binding xBinding = new Binding
    {
        Source = ellipse,
        Path = new PropertyPath(Canvas.LeftProperty)
    };
    line.SetBinding(Line.X1Property, xBinding);

    Binding yBinding = new Binding
    {
        Source = ellipse,
        Path = new PropertyPath(Canvas.TopProperty)
    };
    line.SetBinding(Line.Y1Property, yBinding);

    testcanv.Children.Add(line);
    testcanv.Children.Add(ellipse);

    testcanv.Background = Brushes.Transparent;
    testcanv.MouseMove += adjustRoute;
}

private void adjustRoute(object sender, MouseEventArgs e)
{
    var p = e.GetPosition(testcanv);
    Canvas.SetLeft(ellipse, p.X);
    Canvas.SetTop(ellipse, p.Y);
}

【讨论】:

  • 对不起。我真的很愚蠢。问题是我在绑定分配之后将 X1 和 Y1 行属性设置为当前位置,该分配丢弃了我的绑定并将 X1 和 Y1 属性重置为当前位置。这就是它没有更新的原因...我需要做的就是在绑定分配之前移动那些 X1 Y1 X2 Y2 分配。这解决了问题....
猜你喜欢
  • 2020-06-14
  • 1970-01-01
  • 2011-02-25
  • 2015-11-07
  • 1970-01-01
  • 1970-01-01
  • 2011-02-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多