You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过White框架访问WPF自定义控件的自定义属性?

Absolutely! You can access custom properties of your WPF custom controls in White framework tests—you just need to bridge your control's property to UI Automation first (since White is built on top of UI Automation). Here's a step-by-step solution:

Step 1: Expose Your Custom Property to UI Automation

White can only interact with properties that are visible to UI Automation. For your MyButton control, you'll need to register a custom automation property and create a custom AutomationPeer to expose the property value:

public class MyButton : System.Windows.Controls.Button
{
    // Register a custom automation property for your custom CLR property
    public static readonly AutomationProperty MyCustomButtonPropProperty =
        AutomationProperty.Register(
            "MyCustomButtonProp", 
            typeof(string), 
            typeof(MyButton));

    public MyButton() { }

    public string MyCustomButtonProp 
    { 
        get { return "MyButtonInfo"; } 
    }

    // Override to return our custom automation peer
    protected override AutomationPeer OnCreateAutomationPeer()
    {
        return new MyButtonAutomationPeer(this);
    }

    // Custom peer to expose our property to UI Automation
    private class MyButtonAutomationPeer : ButtonAutomationPeer
    {
        public MyButtonAutomationPeer(MyButton owner) : base(owner) { }

        protected override object GetAutomationPropertyValue(AutomationProperty property)
        {
            if (property == MyCustomButtonPropProperty)
            {
                // Return the value of our custom CLR property
                return ((MyButton)Owner).MyCustomButtonProp;
            }
            // Let the base class handle standard button properties
            return base.GetAutomationPropertyValue(property);
        }
    }
}

Step 2: Access the Custom Property in Your White Test

Once your control exposes the property to UI Automation, you can retrieve it using the AutomationElement property of White's Button (or your custom UIItem):

Option 1: Use White's Standard Button

// Locate your button using White (replace "MyButtonAutomationId" with your actual ID)
var button = window.Get<Button>("MyButtonAutomationId");

// Fetch the custom property value from the underlying AutomationElement
string customPropValue = (string)button.AutomationElement.GetCurrentPropertyValue(MyButton.MyCustomButtonPropProperty);

// Use the value in your test
Console.WriteLine(customPropValue); // Outputs "MyButtonInfo"

Option 2: Create a Custom UIItem (Cleaner Tests)

For more readable test code, create a custom UIItem class that inherits from White's Button and wraps the property access:

public class MyButtonUIItem : Button
{
    public MyButtonUIItem(AutomationElement automationElement, Window window) 
        : base(automationElement, window) { }

    public string MyCustomButtonProp
    {
        get
        {
            return (string)AutomationElement.GetCurrentPropertyValue(MyButton.MyCustomButtonPropProperty);
        }
    }
}

Then use it in your test:

var myButton = window.Get<MyButtonUIItem>("MyButtonAutomationId");
Console.WriteLine(myButton.MyCustomButtonProp); // Outputs "MyButtonInfo"

Why This Works

White's documentation focuses more on basic control interactions and nested controls, but accessing custom properties requires first making those properties visible to UI Automation—this is the missing piece you were looking for. Since White uses UI Automation under the hood, once your property is exposed, you can access it via the underlying AutomationElement.

内容的提问来源于stack exchange,提问作者user1275513

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:13:57