【问题标题】:Specific binding in C#/WPFC#/WPF 中的特定绑定
【发布时间】:2011-05-23 01:40:44
【问题描述】:

这又是我的绑定 :)

这就是问题所在,我需要在我的 GRID 上使用 LINE 连接两个控件。所以我可以用鼠标移动 GRID 和 LINE 周围的控件之一,连接这两个控件的控件必须在移动时立即替换。

有什么想法吗?我认为在每 1px 移动上计算 LINE 的新 X1,Y1,X2,Y2 是不可能的而且非常无速度......所以我认为有办法为 LINE 的 X1,Y1,X2,Y2 创建 BINDINGS,例如:

x1={Binding firstCtrl.Position.X+firstCtrl.Width/2}

但即使在 XAML 中,这种绑定(对我而言)也非常困难,但我需要在 C# 代码中动态创建这种绑定。

再次需要您的建议,如何在 C# 中创建这样的绑定,或者还有其他方法可以实现我的想法?

P.S.“我不需要完整的代码,只需要描述,也许还有简单的代码示例。”

非常感谢。

【问题讨论】:

    标签: c# xaml binding


    【解决方案1】:

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

    Here 是一篇很好的博客文章,讨论了它们的使用。

    【讨论】:

    • 好的,很有趣,但是如何在 C# 代码中创建这样的绑定呢?
    • 是的,你的想法正是我所需要的 :) 非常感谢,只剩下一个问题了。
    【解决方案2】:

    试试这个:

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

    编辑:

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

    希望对您有所帮助。

    【讨论】:

    • 哈哈!这是工作,如果我拖动第二个椭圆,第一个椭圆非常有趣:) 无论如何,我怎么能从 C# 代码中进行这样的绑定?
    • 是的,它仍然很笨重,我指望你让它变得更好:) PS:不要将它们拖出屏幕,否则你将永远无法找回它们。呵呵
    • 抱歉错过了您的问题。我对在代码中创建绑定不是很熟悉——我总是做声明式绑定——如果我发现了什么,我会告诉你的。
    猜你喜欢
    • 1970-01-01
    • 2017-07-18
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-01
    • 2011-03-18
    • 1970-01-01
    相关资源
    最近更新 更多