C#/WPF中的特定绑定

C#/WPF中的特定绑定,c#,xaml,binding,C#,Xaml,Binding,这又是我的绑定:) 这里是问题,我需要在我的网格上有两个控制连接线。所以我可以用鼠标移动网格和线周围的一个控件,连接这两个控件的控件必须在移动时立即替换 有什么想法吗?我认为每1px移动一次就计算出新的X1,Y1,X2,Y2是不可能的,而且速度非常慢。。。所以我认为有一种方法可以为行的X1、Y1、X2、Y2创建绑定,例如: x1={Binding firstCtrl.Position.X+firstCtrl.Width/2} 但即使在XAML中,这样的绑定也非常困难(对我来说),但我需要在C#

这又是我的绑定:)

这里是问题,我需要在我的网格上有两个控制连接线。所以我可以用鼠标移动网格和线周围的一个控件,连接这两个控件的控件必须在移动时立即替换

有什么想法吗?我认为每1px移动一次就计算出新的X1,Y1,X2,Y2是不可能的,而且速度非常慢。。。所以我认为有一种方法可以为行的X1、Y1、X2、Y2创建绑定,例如:

x1={Binding firstCtrl.Position.X+firstCtrl.Width/2}
但即使在XAML中,这样的绑定也非常困难(对我来说),但我需要在C#代码中动态创建这样的绑定

再次需要你的建议,如何在C#中创建这样的绑定,或者有另一种方法来实现我的想法

我不需要完整的代码,只需要描述,也许还有简单的代码示例

非常感谢。

您可以使用来收集感兴趣的点,并根据需要返回X或Y点

这是一篇讨论它们使用的好博客文章。

试试这个:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <ResourceDictionary>
        <ControlTemplate x:Key="template1">
            <Ellipse Width="30" Margin="-15 -15" Height="30" Fill="Black"/>
        </ControlTemplate>
    </ResourceDictionary>
</Window.Resources>

<Canvas Name="myCanvas">
    <Thumb Name="myThumb" DragDelta="onDragDelta" Canvas.Left="0" Canvas.Top="30" Template="{StaticResource template1}"/>
    <Thumb Name="myThumb2" DragDelta="onDragDelta" Canvas.Left="400" Canvas.Top="30" Template="{StaticResource template1}"/>
    <Line X1="{Binding ElementName=myThumb,Path=(Canvas.Left)}" Y1="{Binding ElementName=myThumb,Path=(Canvas.Top)}" X2="{Binding ElementName=myThumb2,Path=(Canvas.Left)}" Y2="{Binding ElementName=myThumb2,Path=(Canvas.Top)}" Stroke="Black" />
</Canvas>

代码隐藏:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        void onDragDelta(object sender, DragDeltaEventArgs e)
        {
            var i = sender as UIElement;
            if (i != null)
            {
                Canvas.SetLeft(i, Canvas.GetLeft(i) + e.HorizontalChange);
                //Canvas.SetTop(myThumb, Canvas.GetTop(myThumb) + e.VerticalChange);
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Controls.Primitives;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间WpfApplication1
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
void onDragDelta(对象发送器,DragDeltaEventArgs e)
{
var i=发送方作为UIElement;
如果(i!=null)
{
Canvas.SetLeft(i,Canvas.GetLeft(i)+e.HorizontalChange);
//Canvas.SetTop(myThumb,Canvas.GetTop(myThumb)+e.VerticalChange);
}
}
}
}
编辑:

说明:这有两个可拖动的圆(仅x轴)和一条连接这两个圆的线。我已经为X-Num和Y-Num属性创建了一个绑定,并将它们绑定到thumb对象的Canvas.Top/Left附加属性(使用ElementName绑定)。这会将线的两端放置在每个对象的0,0坐标中。所以我用一个负的边距把圆的中心放在0,0


希望有帮助。

好的,非常有趣,但是如何在C代码中创建这样的绑定?是的,你的想法正是我需要的:)非常感谢,只剩下一个问题。哈哈!这就是工作,如果我拖动第二个椭圆,第一个椭圆非常有趣:)不管怎样,我怎么能从C代码中进行这样的绑定?是的,它仍然很笨重,我指望你能做得更好:)PS:不要把它们拖到屏幕外,否则你永远也拿不回来。hehehesorry没听到你的问题。我不太熟悉在代码中创建绑定-我总是使用声明性绑定-如果我发现任何东西,我会告诉您。以下是关于绑定的内容: