datatemplate内的绑定边距未更新

时间:2017-01-06 00:16:40

标签: c# wpf xaml mvvm user-controls

我试图在itemscontrol的datatemplate中绑定我的usercontrols(在其代码后面有鼠标左移btn逻辑)边缘。然而,当绑定在datatemplate内部时,它不会在它后面的网格上移动时自动更新(它本身也是一个用户控件,并且在其代码后面的鼠标中间按钮逻辑上移动)。

这些代码背后的代码是:(在网格中它对中间鼠标作出反应)

private void Control_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
            isDragging = true;
            var draggableControl = sender as UserControl;
            lasttransform = new Point(0, 0);
            clickPosition = e.GetPosition(this.Parent as UIElement);
            draggableControl.CaptureMouse();
    }

    private void Control_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (isDragging == true)
        {
            isDragging = false;
            var draggable = sender as UserControl;
            draggable.ReleaseMouseCapture();



            completetransform = Point.Add(completetransform,new Vector( lasttransform.X, lasttransform.Y));
            lasttransform = new Point(0, 0);
        }
    }

    private void Control_MouseMove(object sender, MouseEventArgs e)
    {
        var draggableControl = sender as UserControl;

        if (isDragging && draggableControl != null && draggableControl.GetType() == typeof(BaseNode))
        {
            Point currentPosition = e.GetPosition(this.Parent as UIElement);
            currentPosition = new Point(currentPosition.X, currentPosition.Y);


            var transform = draggableControl.RenderTransform as TranslateTransform;
            if (transform == null)
            {
                transform = new TranslateTransform();
                draggableControl.RenderTransform = transform;
            }

            transform.X = completetransform.X + (currentPosition.X - clickPosition.X);
            transform.Y = completetransform.Y + (currentPosition.Y - clickPosition.Y);

            lasttransform = new Point((currentPosition.X - clickPosition.X), (currentPosition.Y - clickPosition.Y));
        }
    }
}

当我现在想要在某些窗口中显示我的控件时:

<Grid>
    <my:EventGrid x:Name="XDisplayedEventGrid" Margin="-20" DataContext="{Binding DisplayedEventGrid}"/>
    <Grid Background="Red" Width="100" Height="100" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="{Binding DisplayedEventGrid.ActualTransform, Converter={StaticResource ResourceKey=PointToMarginConverter}}"/>

    <my:BaseNode HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Height="100" Margin="{Binding DisplayedEventGrid.ActualTransform, Converter={StaticResource ResourceKey=PointToMarginConverter}}"/>
    <ItemsControl ItemsSource="{Binding DisplayedNodes}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <my:BaseNode HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Height="100" Margin="{Binding  ElementName=XDisplayedEventGrid, Path=ActualTransform, Converter={StaticResource ResourceKey=PointToMarginConverter}}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

我的:EventGrid&#39;是可移动网格,&#39; my:BaseNode&#39;是一个控制测试一切和DataTemplate&gt;我的:BaseNode ... /&gt; / DataTemplate中&GT;是这些用户控件的实际显示控件。

会发生什么:

1)当你移动可视画笔后面的网格时,看起来你可以无限滚动。

2)网格背景=&#34;红色&#34;&#39;留在事件网格上的位置。

3)边距被绑定到eventgrid的移动坐标,该坐标由转换器转换为边距,因此对于用户来说,控件在网格上保持原位并且如果移动到视线之外则移出视图用户查看网格的另一部分。

4)当一个节点被移动时,它的RenderTransform被设置为可以放置并绑定到边距,同时能够使用RenderTransform在本地重新定位它。

我现在的问题是,当测试节点没有DataContext绑定时,它具有网格的行为并相应地移动,但如果它有一个集合,它就像datatemplate中的节点一样,并保持在0,0窗口。

节点的Datacontext是一个空类,它导出了INotifyPropertyChanged的标准实现。

如果没有在datatemplate中绑定datacontext,它仍然具有相同的错误行为。

我的错误在哪里 有没有更好的控制来存储外部marged用户控件?

由于

ShinuSha

0 个答案:

没有答案