【发布时间】:2023-03-23 13:30:01
【问题描述】:
我有一个 ItemsControl,它将 UserControl 显示为 ItemTemplate。它有一个 Canvas ItemsPanel。
<ItemsControl ItemsSource="{Binding Path=MyItems}" Margin="200,20,0,0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:MyControl Margin="10,10,10,10"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Height="2000" Width="2000"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
我想在鼠标拖动时将控件移动到画布上
MyControl 有一个行为:
<UserControl x:Class="MyControl">
<StackPanel x:Name="LayoutRoot" >
<Grid Background="LightBlue" Height="20">
</StackPanel>
<Interactivity:Interaction.Behaviors>
<Behavior:DragControlBehavior />
</Interactivity:Interaction.Behaviors>
</UserControl>
DragControlBehavior 将鼠标移动上的 Canvas 附加属性设置在控件上
[更新] - 这是 Behavior 的完整源代码
public class DragControlBehavior : Behavior<MyControl>
{
private DependencyObject _parent;
private bool _isMouseCaptured = false;
protected override void OnAttached()
{
AssociatedObject.MouseLeftButtonDown += (sender, e) =>
{
_isMouseCaptured = true;
};
AssociatedObject.MouseLeftButtonUp += (sender, e) =>
{
_isMouseCaptured = false;
};
AssociatedObject.MouseMove += (sender, e) =>
{
if (_isMouseCaptured)
{
if (_parent == null)
{
_parent = VisualTreeHelper.GetParent(AssociatedObject);
while (_parent.GetType() != typeof(Canvas))
_parent = VisualTreeHelper.GetParent(_parent);
}
var pointOnCanvas = e.GetPosition((Canvas)_parent);
Canvas.SetTop(AssociatedObject, pointOnCanvas.Y);
Canvas.SetLeft(AssociatedObject, pointOnCanvas.X);
}
};
}
如果我在 Canvas 上单独使用 MyControl 的实例,它可以工作,但如果 ItemsControl 中有 MyControl 的集合,它们不会在 MouseMove 上移动
在 WPF 中,我会使用 ItemContainerStyle,但在 SL 中它不可用:
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding Left}"/>
<Setter Property="Canvas.Top" Value="{Binding Top}"/>
</Style>
</ItemsControl.ItemContainerStyle>
【问题讨论】:
-
你能告诉我们你的
DragControlBehavior类的完整源代码吗?否则,我们很难重现您的问题。 -
附加行为的完整源代码
标签: silverlight silverlight-4.0 canvas itemscontrol