gpt4 book ai didi

wpf - Rx DragBehavior->为什么我的元素不动

转载 作者:行者123 更新时间:2023-12-03 10:21:10 25 4
gpt4 key购买 nike

我根据常规的拖放示例编写了一个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>

最佳答案

稍微更改您的模板...这将对您有所帮助。

<DataTemplate>
<!-- change background to transparent -->
<StackPanel Background="Transparent">
<e:Interaction.Behaviors>
<h:DragBehavior />
</e:Interaction.Behaviors>
<!-- Prevent yout textBlock from capturing mouse events -->
<!-- by setting is HitTestVisible to false -->
<TextBlock Text="{Binding X}" IsHitTestVisible="False" />
</StackPanel>
</DataTemplate>

关于wpf - Rx DragBehavior->为什么我的元素不动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5502853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com