如何在 XAML/WPF 中设置鼠标光标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35311116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:40:06  来源:igfitidea点击:

How to set Mouse Cursor in XAML / WPF?

wpfxamlwpf-controls

提问by Kylo Ren

How can I set the Mouse cursor in xaml?

如何在 xaml 中设置鼠标光标?

What's the use of Cursor property in every control? Please don't answer as Cursor="Arrow"cause this is not working.

每个控件中 Cursor 属性的用途是什么?请不要回答,Cursor="Arrow"因为这不起作用。

Only way I can do it right now is from code behind by Mouse.OverrideCursor. Can I do it simply using XMAL?

我现在能做到的唯一方法是从 Mouse.OverrideCursor. 我可以简单地使用 XMAL 来完成吗?

I have a Hierarchy of controls where there is a GridSplitterin between somewhere. I'm trying to set the Cursor to SizeNSbut it's set to default as default Arrow. What Should I do?

我有一个控件层次结构,其中有一个GridSplitter介于两者之间的地方。我正在尝试将 Cursor 设置为,SizeNS但它被设置为 default 作为 default Arrow。我该怎么办?

回答by Kylo Ren

In WPF Cursorcreates problem when controls are declared in hierarchy and properties get overwritten.

在 WPF 中,Cursor当在层次结构中声明控件并且属性被覆盖时,会产生问题。

If you strictly want to set Cursorin a control use ForceCursorproperty of FrameworkElementclass.

如果您严格想Cursor在类的控件使用ForceCursor属性中进行设置FrameworkElement

Syntax:

句法:

    <StackPanel Name="CursorForced" ForceCursor="true" Cursor="Hand">
        <Label>Cursors Forced</Label>
        <TextBox>Fill me in!</TextBox>
    </StackPanel>

In above example if I don't use ForceCursorthe Cursorwill be different over TextBoxnot as I defined in parent control.

在上面的例子中,如果我不使用ForceCursorCursor将会TextBox与我在父控件中定义的不同。

MSDN link to How to Force Cursor

MSDN 链接到如何强制光标