如何正确设置DataTemplate控件的DataContext

时间:2015-09-22 13:48:23

标签: c# wpf xaml datacontext

这是以下问题:Items added to ItemsControl not using ItemTemplate

我有2个类应该处理对象的移动和调整大小(MoveThumbResizeThumb)。模板正在应用于ItemsControl中的对象,并且应该转换对象的函数被触发但我无法使其工作。

ItemsControl绑定到'{1}}'DashboardItems',此时只是返回ObservableCollection的X和Y的简单对象。我可能会在将来添加它们,但我希望首先使用基础知识

Canvas.SetTop/Left'项目中的比较'始终为空。 MoveThumb_DragDelta有同样的问题。 发件人是ResizeThumbMoveThumbsender.DataContext

我的问题是:我应该如何正确设置模板和/或类的数据上下文,以便ContentPresenter可以获得触发它的控件?

编辑: 将MoveThumb_DragDelta更改为Control item = this.DataContext as Control;已使运动正常,但调整大小不起作用,因为UIElement不包含ActualHeight / Width或MinHeight / Width。

UIElement item = this.DataContext as UIElement;

MoveThumb

public class MoveThumb : Thumb { public MoveThumb() { DragDelta += new DragDeltaEventHandler(this.MoveThumb_DragDelta); } private void MoveThumb_DragDelta(object sender, DragDeltaEventArgs e) { Control item = this.DataContext as Control; if (item != null) { double left = Canvas.GetLeft(item); double top = Canvas.GetTop(item); Canvas.SetLeft(item, left + e.HorizontalChange); Canvas.SetTop(item, top + e.VerticalChange); } } } :     

MainWindow.xaml

<Window.Resources> <ControlTemplate x:Key="MoveThumbTemplate" TargetType="{x:Type local:MoveThumb}"> <Rectangle Fill="Transparent"/> </ControlTemplate> <ControlTemplate x:Key="ResizeDecoratorTemplate" TargetType="Control"> <Grid> <local:ResizeThumb Height="2" Cursor="SizeNS" Margin="0 -4 0 0" VerticalAlignment="Top" HorizontalAlignment="Stretch"/> <local:ResizeThumb Width="2" Cursor="SizeWE" Margin="-4 0 0 0" VerticalAlignment="Stretch" HorizontalAlignment="Left"/> <local:ResizeThumb Width="2" Cursor="SizeWE" Margin="0 0 -4 0" VerticalAlignment="Stretch" HorizontalAlignment="Right"/> <local:ResizeThumb Height="2" Cursor="SizeNS" Margin="0 0 0 -4" VerticalAlignment="Bottom" HorizontalAlignment="Stretch"/> <local:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="-6 -6 0 0" VerticalAlignment="Top" HorizontalAlignment="Left"/> <local:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="0 -6 -6 0" VerticalAlignment="Top" HorizontalAlignment="Right"/> <local:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="-6 0 0 -6" VerticalAlignment="Bottom" HorizontalAlignment="Left"/> <local:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="0 0 -6 -6" VerticalAlignment="Bottom" HorizontalAlignment="Right"/> </Grid> </ControlTemplate> <DataTemplate DataType="{x:Type local:TableControl}"> <Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"> <local:MoveThumb Template="{StaticResource MoveThumbTemplate}" DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}" Cursor="SizeAll"/> <Control Template="{StaticResource ResizeDecoratorTemplate}"/> <Button Content="x" VerticalAlignment="Top" HorizontalAlignment="Right" Width="10" Height="10" Click="Button_Click"/> <Ellipse Fill="Red" IsHitTestVisible="False" Height="100" Width="100"/> </Grid> </DataTemplate> <DataTemplate DataType="{x:Type local:GraphControl}"> <Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"> <local:MoveThumb Template="{StaticResource MoveThumbTemplate}" DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}" Cursor="SizeAll"/> <Control Template="{StaticResource ResizeDecoratorTemplate}"/> <Button Content="x" VerticalAlignment="Top" HorizontalAlignment="Right" Width="10" Height="10" Click="Button_Click"/> <Ellipse Fill="Green" IsHitTestVisible="False" Height="100" Width="100"/> </Grid> </DataTemplate> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <ribbon:Ribbon Margin="0,-22,0,0" Grid.Row="0"> <ribbon:RibbonTab Header="Dashboard"> <ribbon:RibbonGroup Header="Customise"> <ribbon:RibbonMenuButton Label="New" FontSize="20" Height="60" Width="60"> <ribbon:RibbonMenuItem Header="Graph" Click="NewGraph"/> <ribbon:RibbonMenuItem Header="Table" Click="NewTable"/> </ribbon:RibbonMenuButton> </ribbon:RibbonGroup> </ribbon:RibbonTab> </ribbon:Ribbon> <ItemsControl Name="dashboardControls" ItemsSource="{Binding Path=CanvasContents}" Grid.Row="1"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Canvas/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemContainerStyle> <Style> <Setter Property="Canvas.Top" Value="{Binding Path=Y}"/> <Setter Property="Canvas.Left" Value="{Binding Path=X}"/> </Style> </ItemsControl.ItemContainerStyle> </ItemsControl> </Grid>

MainWindow.xaml.cs

public partial class MainWindow : Window { public static Dashboard dashboard; public MainWindow() { dashboard = new Dashboard(); InitializeComponent(); this.DataContext = dashboard; } private void NewGraph(object sender, RoutedEventArgs e) { dashboard.CanvasContents.Add(new GraphControl()); } private void NewTable(object sender, RoutedEventArgs e) { dashboard.CanvasContents.Add(new TableControl()); } }

Dashboard.cs

public class Dashboard: INotifyPropertyChanged { ObservableCollection<DashboardItem> _canvasContents; public Dashboard() { _canvasContents = new ObservableCollection<DashboardItem>(); } public ObservableCollection<DashboardItem> CanvasContents { get { return _canvasContents; } } }

DashboardItem.cs

1 个答案:

答案 0 :(得分:0)

我还没有尝试过,但http://www.codeproject.com/Articles/22952/WPF-Diagram-Designer-Part似乎有可能找到答案。

编辑: 我从评论中看到,问题在于从Canvas转移到ItemsControl。在该上下文中似乎存在Canvas.TopCanvas.Left的问题,以及设置某些样式属性的解决方案:Dragable objects in WPF in an ItemsControl?