使用BackgroundWorker线程的WPF等待光标

时间:2009-04-17 22:07:23

标签: .net wpf multithreading backgroundworker mouse-cursor

我希望在BackgroundWorker进程在另一个线程中运行时显示沙漏光标并禁用该窗口。

这就是我正在做的事情:

Private Sub MyButton_Click(...)
    Dim box As New AnotherWpfWindow()
    box.Owner = Me
    ...
    box.ShowDialog()
    If (box.DialogResult.GetValueOrDefault = True) Then
        Me.IsEnabled = False
        Me.Cursor = Cursors.Wait
        MyBackgroundWorker.RunWorkerAsync()
    End If
End Sub

Private Sub MyBackgroundWorker_RunWorkerCompleted(...)
    UpdateInterface()
    Me.IsEnabled = True
    Me.Cursor = Cursors.Arrow
End Sub

窗口会像我想要的那样被禁用,但光标仍然是箭头。我怎样才能使它成为等待光标?

根据这个问题似乎适用于vg1890Disabling all but one control in a WPF window

2 个答案:

答案 0 :(得分:11)

另一种方法是全局更改光标,是..

Mouse.OverrideCursor = Cursors.Wait;

//Do something
//...

Mouse.OverrideCursor = null;

答案 1 :(得分:8)

这里似乎发生的事情是WPF忽略了禁用窗口上的Cursor设置。以下解决方法似乎有效:不禁用窗口本身,而是禁用窗口的内容

C#:

((UIElement)Content).IsEnabled = false;
Cursor = Cursors.Wait;

// and in RunWorkerCompleted handler:
((UIElement)Content).IsEnabled = true;
Cursor = Cursors.Arrow;

Visual Basic:

DirectCast(Content, UIElement).IsEnabled = False
Cursor = Cursors.Wait

' and in RunWorkerCompleted handler:'
DirectCast(Content, UIElement).IsEnabled = True
Cursor = Cursors.Arrow