WPF:动态创建的行不会出现在定义的位置

时间:2017-02-08 12:26:49

标签: wpf line itemtemplate itemsource

我有一个记录行的起点和终点的类。这个类的元素被添加到一个Observable Collection中,它作为我的ItemsControl的ItemsSource。

以下是XAML的相关部分:

<ItemsControl Name="PathControl">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Line
                                Name="Path"
                                Stroke="Red"
                                StrokeThickness="4"
                                X1="{Binding
                                    StartCoordinates.X,
                                    Mode=TwoWay,
                                    UpdateSourceTrigger=PropertyChanged}"

                                Y1="{Binding
                                    StartCoordinates.Y,
                                    Mode=TwoWay,
                                    UpdateSourceTrigger=PropertyChanged}"
                                X2="{Binding
                                    EndCoordinates.X,
                                    Mode=TwoWay,
                                    UpdateSourceTrigger=PropertyChanged}"

                                Y2="{Binding
                                    endCoordinates.Y,
                                    Mode=TwoWay,
                                    UpdateSourceTrigger=PropertyChanged}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

这是相应的类:

public class VisualPath: INotifyPropertyChanged
{

    public VisualPath(Point start, Point end)
    {
        this.startCoordinates = start;
        this.endCoordinates = end;
    }


    Point startCoordinates;
    public Point StartCoordinates
    {
        get
        {
            return startCoordinates;
        }

        set
        {
            startCoordinates = value;
            OnPropertyChanged();
        }
    }

    Point endCoordinates;
    public Point EndCoordinates
    {
        get
        {
            return endCoordinates;
        }

        set
        {
            endCoordinates = value;
            OnPropertyChanged();
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

这是我的MainWindow的相关部分:

public MainWindow()
{          
    InitializeComponent();
    PathControl.ItemsSource = PathElements = new ObservableCollection<VisualPath>();           
}

我想创建多对按钮并将它们与线条连接起来。为此,我使用单击事件记录两个按钮相对于网格的绝对位置。我将第一个按钮的位置存储在Position变量(tempPoint)中,当我点击第二个按钮(目标)时,应该将第一个按钮的位置添加到集合中(这会发生)。它的X1和Y1(起始点)来自tempPoint,第二个按钮的位置是X2和Y2(终点)。

以下是处理点击事件的方法:

private void firstButton_Click(object sender, RoutedEventArgs e)
{
    Button pathInitiator = sender as Button;
    if (pathInitiator != null)
    {
        tempPoint = pathInitiator.TranslatePoint(new Point(0, 0), MainGrid);
    }
    else
    {
        MessageBox.Show("Not Button");
    }
}

private void secondButton_Click(object sender, RoutedEventArgs e)
{
    Button pathTerminator = sender as Button;
    if (pathTerminator != null)
    {
        GeneralTransform end = pathTerminator.TransformToVisual(this);
        Point endPoint = end.Transform(new Point(0, 0));
        PathElements.Add(new VisualPath(tempPoint, endPoint));
        //PathElements.Add(new VisualPath(new Point(0, 0), new Point(200, 200)));
    }

}

尽管通常正确捕获第一个按钮的第一个实例的位置,但该行的终点始终是错位的,并且第一个按钮的任何其他实例也是如此。显示的行总是显示为翻转或移动,即使我尝试在彼此的顶部添加多行(如注释掉的行所示),新行总是出现在前一行之下。我的接线是否坏了,或者有什么我不了解WPF中的线路如何工作?

修改 感谢评论,现在该对的第一个实例应该按照它的方式工作:

The edges of the buttons are connected by a line as expected

但该对的任何其他实例都是错位的: The pair is not connected, although the very same method is used

1 个答案:

答案 0 :(得分:0)

如果您希望这些行最终位于彼此之上,则可以将ItemsPanelTemplate的{​​{1}}设置为ItemsControl

Grid