简体   繁体   English

如何更改自定义控件 IsMouseOver 的属性值

[英]How do I change the property value of a custom control IsMouseOver

  • i create custom frameworkelement ( notifyicon ).我创建自定义框架元素( notifyicon )。

  • i listening windows message callback.我正在听 windows 消息回调。

  • i want Onmouseenter changed ismouseover,but ismouseover is RegisterReadOnly.我希望 Onmouseenter 更改 ismouseover,但 ismouseover 是 RegisterReadOnly。

  • i cannot changed我无法改变

     private IntPtr CallBack(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam) { if (IsLoaded) { var wm = lparam.ToInt32(); switch (wm) { case WM.LBUTTONDBLCLK: OnMouseDoubleClick(); break; case WM.LBUTTONDOWN: OnMouseDown(MouseButton.Left); break; case WM.LBUTTONUP: OnMouseUp(MouseButton.Left); break; case WM.RBUTTONDOWN: OnMouseDown(MouseButton.Right); break; case WM.RBUTTONUP: OnMouseUp(MouseButton.Right); break; case WM.MOUSEMOVE: OnMouseEnter(); OnMouseMove(); break; } } return User32.DefWindowProc(hwnd, msg, wparam, lparam); } private void OnMouseEnter() { IsMouseOver = true;// cannot changed }

You don't.你没有。 As the docs states, you shouldn't really do this:正如文档所述,您不应该这样做:

Some of the dependency properties defined in the WPF framework are read-only. WPF 框架中定义的一些依赖属性是只读的。 The typical reason for specifying a read-only dependency property is that these are properties that should be used for state determination, but where that state is influenced by a multitude of factors, but just setting the property to that state isn't desirable from a user interface design perspective.指定只读依赖属性的典型原因是这些属性应该用于 state 确定,但 state 受多种因素影响,但仅将属性设置为 Z9ED39E2EA931586B6EZ985A6 是不可取的。用户界面设计视角。 For example, the property IsMouseOver is really just surfacing state as determined from the mouse input.例如,属性 IsMouseOver 实际上只是根据鼠标输入确定 state 表面。 Any attempt to set this value programmatically by circumventing the true mouse input would be unpredictable and would cause inconsistency.通过规避真正的鼠标输入以编程方式设置此值的任何尝试都是不可预测的,并且会导致不一致。

You can set the dependency property using reflection:可以使用反射设置依赖属性

DependencyPropertyKey key = (DependencyPropertyKey)typeof(UIElement).GetField("IsMouseOverPropertyKey",
               BindingFlags.NonPublic | BindingFlags.Static).GetValue(this);

SetValue(key, true);

This will however not affect the IsMouseOver CLR property which gets its value from an internal flag field rather than from the dependency property:然而,这不会影响IsMouseOver CLR 属性,该属性从内部标志字段而不是依赖属性获取其值:

public bool IsMouseOver
{
    get
    {
        return ReadFlag(CoreFlags.IsMouseOverCache);
    }
}

So if want to customize the way to detect whether the mouse is over your control, you'd better create your own property that you can set however you want.因此,如果要自定义检测鼠标是否在您的控件上方的方式,您最好创建自己的属性,您可以根据需要设置该属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在IsMouseOver触发器上更改UserControl的子元素的属性? - How to change a property of a UserControl's child element on a IsMouseOver trigger? 'ismouseover' 属性停留在 'enteractions/exitactions' 模式,如何更改按钮的焦点? 请注意图片(GIF) - The 'ismouseover' attribute stays in 'enteractions/exitactions' mode , how do I change the focus of the button ? please note the image (GIF) 如何正确公开自定义控件上的属性? - How do I correctly Expose a property on a custom control? WPF控件模板 - 如何创建自定义属性? - WPF Control Templating - How do I create a custom property? 如何在自定义控件中对列表属性进行绑定工作? - How do I make binding work for a list property in a custom control? 如果控件具有属性,则更改控件的属性值 - Change property value of control if the control has the property 如何使IsMouseOver不在乎覆盖控件的其他内容? - How can I make IsMouseOver not care about other things covering up the control? 委托控制价值/财产的变化 - Delegating Control on change of a value/property 如何向消费者隐藏 Xamarin Forms 自定义控件中的属性 - How do I hide a property in a Xamarin Forms custom control from consumers ViewModel如何告诉我自定义控件在我的属性中的更改? - How can ViewModel tell my custom control change in my property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM