我注意到我的 SPListItemCollection listItemCollection = list.GetItems();
for (int i = 0; i < listItemCollection.Count; i++)
{
var columnToUpdate = "MyField";
var item = listItemCollection[i];
string internalName = item.Fields[columnToUpdate].InternalName;
item[internalName] = "";
item.Update();
}
事件处理程序仅在鼠标指针在窗口中时执行。我希望它在指针位于窗口之外时运行处理程序。
我的程序中只有一个窗口,其XAML的结构如下:
MouseWheel
答案 0 :(得分:0)
为了在光标离开窗口时接收鼠标事件,窗口需要“捕获”鼠标。它通过将自己传递给Mouse.Capture()
方法来实现这一点。
请注意,如果您的程序窗口失去激活(即用户任务切换到某个其他程序),它将失去鼠标捕获。如果您想在以后重新激活窗口时再次自动跟踪鼠标滚轮,则需要专门处理(即处理Activated
事件)。
最后请注意,启用鼠标捕获后,通过鼠标与窗口的正常交互不起作用, Alt - F4 也不起作用。您需要为用户提供一些额外与程序交互的机制(例如处理键事件)。
下面是一个简单的示例程序,它显示了基本概念:
<强> XAML:强>
<Window x:Class="TestSO30866523CaptureMouseWheel.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:TestSO30866523CaptureMouseWheel"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Activated="Window_Activated"
KeyDown="Window_KeyDown"
MouseWheel="Window_MouseWheel"
Title="MainWindow" Height="350" Width="525">
<StackPanel x:Name="stackPanel1">
<TextBlock Text="The window cannot be closed while mouse is captured."/>
<TextBlock Text="Press Escape to stop capture. Press Return to resume capture."/>
<TextBlock Text="{Binding Value, StringFormat=Value: {0}}"/>
<TextBlock Text="{Binding MouseCaptured, StringFormat=Mouse is captured: {0}}"/>
</StackPanel>
</Window>
<强> C#:强>
public partial class MainWindow : Window
{
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
"Value", typeof(int), typeof(MainWindow), new PropertyMetadata(0));
public static readonly DependencyProperty MouseCapturedProperty = DependencyProperty.Register(
"MouseCaptured", typeof(bool), typeof(MainWindow));
public int Value
{
get { return (int)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public bool MouseCaptured
{
get { return (bool)GetValue(MouseCapturedProperty); }
set { SetValue(MouseCapturedProperty, value); }
}
private readonly IInputElement _captureElement;
public MainWindow()
{
InitializeComponent();
_captureElement = this;
Mouse.AddGotMouseCaptureHandler((DependencyObject)_captureElement, stackPanel1_GotLostMouseCapture);
Mouse.AddLostMouseCaptureHandler((DependencyObject)_captureElement, stackPanel1_GotLostMouseCapture);
Mouse.Capture(_captureElement);
MouseCaptured = Mouse.Captured != null;
}
private void stackPanel1_GotLostMouseCapture(object sender, MouseEventArgs e)
{
MouseCaptured = Mouse.Captured != null;
}
private void Window_MouseWheel(object sender, MouseWheelEventArgs e)
{
Value += e.Delta;
}
private void Window_Activated(object sender, EventArgs e)
{
Mouse.Capture(_captureElement);
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Escape:
Mouse.Capture(null);
break;
case Key.Return:
Mouse.Capture(_captureElement);
break;
}
}
}
捕获鼠标时,无论光标放在何处,窗口都会响应鼠标滚轮事件。未捕获鼠标时,仅当光标位于窗口上时,它才会响应鼠标滚轮事件。