Rx DragBehavior ->为什么我的元素不动
我根据一般的拖放示例编写了一个 DragBehavior。虽然这在不处于行为中的情况下也可以工作,但当我将代码放入行为中时它不会移动。
行为:
public class DragBehavior : Behavior<UIElement>
{
protected override void OnAttached()
{
Window mainCanvas = Application.Current.MainWindow;
var mouseDown = from evt in AssociatedObject.GetMouseLeftButtonDown()
select evt.EventArgs.GetPosition(mainCanvas);
var mouseUp = from evt in AssociatedObject.GetMouseLeftButtonUp()
select evt.EventArgs.GetPosition(mainCanvas);
var mouseMove = from evt in AssociatedObject.GetMouseMove()
select evt.EventArgs.GetPosition(mainCanvas);
var q = from start in mouseDown
from delta in mouseMove.StartWith(start).TakeUntil(mouseUp)
.Let(mm => mm.Zip(mm.Skip(1),(pre,cur) =>
new{ X= cur.X - pre.X, Y= cur.Y - pre.Y}))
select delta;
q.Subscribe(value =>
{
Canvas.SetLeft(AssociatedObject, Canvas.GetLeft(AssociatedObject) + value.X);
Canvas.SetTop(AssociatedObject, Canvas.GetTop(AssociatedObject) + value.Y);
});
}
}
帮助方法:
public static IObservable<IEvent<MouseEventArgs>> GetMouseMove(
this UIElement inputElement)
{
return Observable.FromEvent(
(EventHandler<MouseEventArgs> h) => new MouseEventHandler(h),
h => inputElement.MouseMove += h,
h => inputElement.MouseMove -= h);
}
public static IObservable<IEvent<MouseButtonEventArgs>>
GetMouseLeftButtonDown(this UIElement inputElement)
{
return Observable.FromEvent(
(EventHandler<MouseButtonEventArgs> genericHandler) =>
new MouseButtonEventHandler(genericHandler),
h => inputElement.MouseLeftButtonDown += (h),
h => inputElement.MouseLeftButtonDown -= (h));
}
public static IObservable<IEvent<MouseButtonEventArgs>>
GetMouseLeftButtonUp(this UIElement inputElement)
{
return Observable.FromEvent(
(EventHandler<MouseButtonEventArgs> genericHandler) =>
new MouseButtonEventHandler(genericHandler),
h => inputElement.MouseLeftButtonUp += h,
h => inputElement.MouseLeftButtonUp -= h);
}}
<Window x:Class="DesignerTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:DesignerTest.ViewModels"
xmlns:h="clr-namespace:DesignerTest.Helpers"
xmlns:e="http://schemas.microsoft.com/expression/2010/interactivity"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<vm:WindowViewModel />
</Window.DataContext>
<Grid>
<ItemsControl ItemsSource="{Binding Path=Shapes}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<e:Interaction.Behaviors>
<h:DragBehavior />
</e:Interaction.Behaviors>
<TextBlock Text="{Binding X}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding Path=X,Mode=TwoWay}" />
<Setter Property="Canvas.Top" Value="{Binding Path=Y,Mode=TwoWay}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</Grid>
I wrote a DragBehavior based on the general Drag and Drop examples. While this is working without being in a Behavior, it is not moving when I place the code in a behavior.
Behavior:
public class DragBehavior : Behavior<UIElement>
{
protected override void OnAttached()
{
Window mainCanvas = Application.Current.MainWindow;
var mouseDown = from evt in AssociatedObject.GetMouseLeftButtonDown()
select evt.EventArgs.GetPosition(mainCanvas);
var mouseUp = from evt in AssociatedObject.GetMouseLeftButtonUp()
select evt.EventArgs.GetPosition(mainCanvas);
var mouseMove = from evt in AssociatedObject.GetMouseMove()
select evt.EventArgs.GetPosition(mainCanvas);
var q = from start in mouseDown
from delta in mouseMove.StartWith(start).TakeUntil(mouseUp)
.Let(mm => mm.Zip(mm.Skip(1),(pre,cur) =>
new{ X= cur.X - pre.X, Y= cur.Y - pre.Y}))
select delta;
q.Subscribe(value =>
{
Canvas.SetLeft(AssociatedObject, Canvas.GetLeft(AssociatedObject) + value.X);
Canvas.SetTop(AssociatedObject, Canvas.GetTop(AssociatedObject) + value.Y);
});
}
}
Helper Methods:
public static IObservable<IEvent<MouseEventArgs>> GetMouseMove(
this UIElement inputElement)
{
return Observable.FromEvent(
(EventHandler<MouseEventArgs> h) => new MouseEventHandler(h),
h => inputElement.MouseMove += h,
h => inputElement.MouseMove -= h);
}
public static IObservable<IEvent<MouseButtonEventArgs>>
GetMouseLeftButtonDown(this UIElement inputElement)
{
return Observable.FromEvent(
(EventHandler<MouseButtonEventArgs> genericHandler) =>
new MouseButtonEventHandler(genericHandler),
h => inputElement.MouseLeftButtonDown += (h),
h => inputElement.MouseLeftButtonDown -= (h));
}
public static IObservable<IEvent<MouseButtonEventArgs>>
GetMouseLeftButtonUp(this UIElement inputElement)
{
return Observable.FromEvent(
(EventHandler<MouseButtonEventArgs> genericHandler) =>
new MouseButtonEventHandler(genericHandler),
h => inputElement.MouseLeftButtonUp += h,
h => inputElement.MouseLeftButtonUp -= h);
}}
<Window x:Class="DesignerTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:DesignerTest.ViewModels"
xmlns:h="clr-namespace:DesignerTest.Helpers"
xmlns:e="http://schemas.microsoft.com/expression/2010/interactivity"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<vm:WindowViewModel />
</Window.DataContext>
<Grid>
<ItemsControl ItemsSource="{Binding Path=Shapes}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<e:Interaction.Behaviors>
<h:DragBehavior />
</e:Interaction.Behaviors>
<TextBlock Text="{Binding X}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding Path=X,Mode=TwoWay}" />
<Setter Property="Canvas.Top" Value="{Binding Path=Y,Mode=TwoWay}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</Grid>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
稍微改变一下你的模板...mb这会对你有帮助..
Change your template a little... mb this will help you..
代码真的在运行吗?向 OnAttached 和您的 Subscribe 闭包添加一个“When Hit”断点,以跟踪实际发生的情况
另外,关于您的实现的一些注意事项:
MutableDisposable
私有成员并分配返回值订阅
它的Disposable
属性。这将允许您取消订阅OnDetached
中的所有内容。TakeUntil
不要求序列具有相同类型。编辑:我认为问题是您要添加 Canvas 属性的
StackPanel
实际上是ItemContainerTemplate
(ContentPresenter
) 元素,而不是Canvas
本身。您应该在行为的OnAttached
方法中遍历树,或者定义自己的ItemContainerTemplate
并将行为附加到ContentPresenter
。Is the code actually running? Add a "When Hit" breakpoint to OnAttached and your Subscribe closure to trace out what's atually happening
Also, a few notes on your implementation:
MutableDisposable
private member and assigning the return value ofSubscribe
to it'sDisposable
property. This will allow you to unsubscribe from everything inOnDetached
TakeUntil
doesn't require that the sequences be of the same type.Edit: I think the problem is that the
StackPanel
you are adding the Canvas properties to is actually the child of theItemContainerTemplate
(ContentPresenter
) element, not of theCanvas
itself. You should either walk the tree in the behavior'sOnAttached
method, or define your ownItemContainerTemplate
and attach the behavior to theContentPresenter
instead.