在我的几个用户控件上,我使用以下命令更改光标
this.Cursor = Cursors.Wait;
当我点击某样东西的时候。
现在,我想在WPF页面上单击一个按钮来做同样的事情。当我将鼠标悬停在按钮上时,光标会变成一只手,但当我单击它时,它不会变成等待光标。我想知道这是不是因为它是一个按钮,或者因为这是一个页面而不是一个用户控件?这似乎是奇怪的行为。
发布于 2008-11-20 22:03:56
当光标位于特定的页面/用户控件上时,是否需要将光标设置为“等待”光标?如果没有,我建议使用Mouse.OverrideCursor
Mouse.OverrideCursor = Cursors.Wait;
try
{
// do stuff
}
finally
{
Mouse.OverrideCursor = null;
}
这将覆盖您的应用程序的光标,而不仅仅是其UI的一部分,因此您所描述的问题就消失了。
发布于 2010-05-18 22:17:04
您可以在按钮上使用数据触发器(带有视图模型)来启用等待光标。
<Button x:Name="NextButton"
Content="Go"
Command="{Binding GoCommand }">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Cursor" Value="Arrow"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsWorking}" Value="True">
<Setter Property="Cursor" Value="Wait"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
下面是view-model中的代码:
public class MainViewModel : ViewModelBase
{
// most code removed for this example
public MainViewModel()
{
GoCommand = new DelegateCommand<object>(OnGoCommand, CanGoCommand);
}
// flag used by data binding trigger
private bool _isWorking = false;
public bool IsWorking
{
get { return _isWorking; }
set
{
_isWorking = value;
OnPropertyChanged("IsWorking");
}
}
// button click event gets processed here
public ICommand GoCommand { get; private set; }
private void OnGoCommand(object obj)
{
if ( _selectedCustomer != null )
{
// wait cursor ON
IsWorking = true;
_ds = OrdersManager.LoadToDataSet(_selectedCustomer.ID);
OnPropertyChanged("GridData");
// wait cursor off
IsWorking = false;
}
}
}
发布于 2013-07-08 22:11:46
如果您的应用程序使用异步的东西,而您正在摆弄鼠标的光标,那么您可能希望只在主UI线程中执行此操作。你可以使用app的Dispatcher线程来实现:
Application.Current.Dispatcher.Invoke(() =>
{
// The check is required to prevent cursor flickering
if (Mouse.OverrideCursor != cursor)
Mouse.OverrideCursor = cursor;
});
https://stackoverflow.com/questions/307004
复制相似问题