我几乎把这件事与一件令人烦恼的事情分开了......
因为ListBox选择在鼠标按下时发生,如果在选择要拖动的最后一个项目时用鼠标向下开始拖动它可以正常工作,但是如果选择要先拖动的所有项目然后单击选择以开始拖动它,您单击的那个未被选中并在拖动后留下.
有关解决这个问题的最佳方法吗?
<DockPanel LastChildFill="True">
<ListBox ItemsSource="{Binding SourceItems}"
SelectionMode="Multiple"
PreviewMouseLeftButtonDown="HandleLeftButtonDown"
PreviewMouseLeftButtonUp="HandleLeftButtonUp"
PreviewMouseMove="HandleMouseMove"
MultiSelectListboxDragDrop:ListBoxExtension.SelectedItemsSource="{Binding SelectedItems}"/>
<ListBox ItemsSource="{Binding DestinationItems}"
AllowDrop="True"
Drop="DropOnToDestination"/>
<DockPanel>
Run Code Online (Sandbox Code Playgroud)
...
public partial class Window1
{
private bool clickedOnSourceItem;
public Window1()
{
InitializeComponent();
DataContext = new WindowViewModel();
}
private void DropOnToDestination(object sender, DragEventArgs e)
{
var viewModel = (WindowViewModel)
e.Data.GetData(typeof(WindowViewModel));
viewModel.CopySelectedItems();
}
private void HandleLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var sourceElement = (FrameworkElement)sender;
var hitItem = sourceElement.InputHitTest(e.GetPosition(sourceElement))
as FrameworkElement;
if(hitItem != null)
{
clickedOnSourceItem = true;
}
}
private void …Run Code Online (Sandbox Code Playgroud) 我有一些使用Rx的代码,从多个线程调用,它执行:
subject.OnNext(value); // where subject is Subject<T>
Run Code Online (Sandbox Code Playgroud)
我希望在后台处理这些值,所以我的订阅是
subscription = subject.ObserveOn(Scheduler.TaskPool).Subscribe(value =>
{
// use value
});
Run Code Online (Sandbox Code Playgroud)
我并不关心哪些线程处理来自Observable的值,只要将工作放入TaskPool并且不阻止当前线程.但是,我在OnNext委托中使用'value'并不是线程安全的.目前,如果很多值正在通过Observable,我正在调用我的OnNext处理程序.
我可以为我的OnNext委托添加一个锁,但这不像Rx的做事方式.当我有多个线程调用时,确保我一次只调用一次OnNext处理程序的最佳方法是什么subject.OnNext(value);?
下面的代码,在.NET 4.5上运行发布配置时,会产生以下输出...
Without virtual: 0.333333333333333
With virtual: 0.333333343267441
Run Code Online (Sandbox Code Playgroud)
(在调试中运行时,两个版本都会给出0.333333343267441结果.)
我可以看到,将一个浮点除以一个short并将其返回一个double可能会在一个点之后产生垃圾.
我的问题是:任何人都能解释为什么在分母中提供短片的属性是虚拟的还是非虚拟的时,结果会有所不同?
public class ProvideThreeVirtually
{
public virtual short Three { get { return 3; } }
}
public class GetThreeVirtually
{
public double OneThird(ProvideThreeVirtually provideThree)
{
return 1.0f / provideThree.Three;
}
}
public class ProvideThree
{
public short Three { get { return 3; } }
}
public class GetThree
{
public double OneThird(ProvideThree provideThree)
{
return 1.0f / provideThree.Three;
}
}
class Program
{
static void Main() …Run Code Online (Sandbox Code Playgroud) 我正在使用一个装饰来展示被拖动元素的"幽灵"......
var adornerLayer = AdornerLayer.GetAdornerLayer(topLevelGrid);
dragAdorner = new DragAdorner(topLevelGrid, itemToDrag);
adornerLayer.Add(dragAdorner);
dragAdorner.UpdatePosition(e.GetPosition(topLevelGrid));
DragDrop.DoDragDrop(sourceItems, viewModel, DragDropEffects.Move);
adornerLayer.Remove(dragAdorner);
itemToDrag = null;
Run Code Online (Sandbox Code Playgroud)
...但是在拖动过程中我无法找到更新装饰者位置的好方法.我最接近的是设置AllowDrop="true"顶级网格并给它一个DragOver处理程序......
private void TopLevelGrid_OnDragOver(object sender, DragEventArgs e)
{
dragAdorner.UpdatePosition(e.GetPosition(topLevelGrid));
}
Run Code Online (Sandbox Code Playgroud)
但这意味着我没有得到DragDropEffects关于光标的正确反馈,即它总是显示DragDropEffects.Move光标而不是DragDropEffects.None直到我超过实际的放置目标.
有谁知道更新装饰位置的更好方法?