[英]Conditional binding in ListBox WPF
I have a listbox in which drag and drop is implemented. 我有一个在其中实现拖放的列表框。 I have bound SelectedItem and SelectedIndex properties.
我已经绑定了SelectedItem和SelectedIndex属性。 The SelectedItem and SelectedIndex properties gets set whenever there is a mouse down event.
每当发生鼠标按下事件时,就会设置SelectedItem和SelectedIndex属性。 How do I prevent it from getting set when there only is a drag and drop operation?
仅进行拖放操作时,如何防止其设置? I tried overiding the previewleftbuttondown but found no success.
我尝试覆盖previewleftbuttondown,但未成功。 Any ideas?
有任何想法吗? I need some thing like:
我需要一些东西:
TimeSpan difference = DateTime.Now - mousePressedTime;
if (difference.TotalSeconds >= 3)
{
// long press
//SelectedIndex and SelectedItem should not be set.
}
else
{
// short press
//SelectedIndex and SelectedItem should be set.
}
I found a solution finally. 我终于找到了解决方案。 But I couldn't do anything with the binding.
但是绑定我什么也做不了。 What I did was I created my own Listbox and hooked the PreviewMouseLeftButtonDown and PreviewMouseLeftButtonUp events.
我所做的是创建了自己的列表框,并挂接了PreviewMouseLeftButtonDown和PreviewMouseLeftButtonUp事件。 What ever logic I was executing in the SelectedItem and SelectedIndex properties of the listbox, I had to move it some where else.
无论我在列表框的SelectedItem和SelectedIndex属性中执行什么逻辑,我都必须将其移动到其他位置。 The code is as follows, hope it gives a better idea:
代码如下,希望能给出更好的主意:
public class DragDropListBox : ListBox
{
private static DateTime mousePressedTime;
public DragDropListBox()
{
this.PreviewMouseLeftButtonDown += PreviewMouseLeftButtonDownHandler;
this.PreviewMouseLeftButtonUp += PreviewMouseLeftButtonUpHandler;
}
private void PreviewMouseLeftButtonDownHandler(object sender, MouseButtonEventArgs e)
{
mousePressedTime = DateTime.Now;
}
private void PreviewMouseLeftButtonUpHandler(object sender, MouseButtonEventArgs e)
{
TimeSpan difference = DateTime.Now - mousePressedTime;
if (difference.TotalSeconds <= 1)
{
// short press
if (SelectedItem != null)
{
// do what ever you have to
}
}
UnselectAll();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.