在 WPF 中查找目标 UIElement 的标签

发布于 2024-08-19 02:58:30 字数 744 浏览 5 评论 0原文

我有以下 XAML:

<StackPanel>
    <Label Target="{Binding ElementName=txtSearch}" Content="_Search:" />
    <TextBox x:Name="txtSearch" />
</StackPanel>

我有一个接受 UIElement 参数的扩展方法,如下所示:

static public class MyExtensionMethods
{
    static public string GetLabelText(this UIElement element)
    {
    }
}

我想要在 GetLabelText 方法内部执行的所有操作是确定针对传递的 UIElement 的标签内容(如果有),并返回文本。例如,以下代码将返回“_Search:”:

string labelText = txtSearch.GetLabelText();

我听说您可以使用 AutomationPeers 来执行此操作,但到目前为止我还没有太多接触 UIAutomation 功能,并且除了空值之外似乎无法返回任何内容从我发现的任何自动化示例上对 GetLabeledBy 的调用。任何有效的答案都将是最有帮助的,但除了您已经在此处看到的内容之外,我宁愿不必在 XAML 中执行任何额外操作。

有什么想法吗?

I have the following XAML:

<StackPanel>
    <Label Target="{Binding ElementName=txtSearch}" Content="_Search:" />
    <TextBox x:Name="txtSearch" />
</StackPanel>

I have an extension method that accepts a UIElement parameter like so:

static public class MyExtensionMethods
{
    static public string GetLabelText(this UIElement element)
    {
    }
}

All I want to do inside of the GetLabelText method is to determine the Content of the Label (if there is one) that is targeting the passed UIElement, and return the text. For example, the following code would return "_Search:":

string labelText = txtSearch.GetLabelText();

I have heard that you can do this using AutomationPeers, but I have not had much exposure to the UIAutomation features as of yet and can't seem to get anything back but null values from calls to GetLabeledBy on any of the Automation examples I've found. Any answer that works would be most helpful, but I'd prefer to not have to do anything extra in my XAML except what you already see here.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离鸿 2024-08-26 02:58:30

除了 Josh Einstein 的解决方案(如果我没记错的话,相当于简单地调用静态方法 AutomationProperties.GetLabeledBy),我看到的此问题的唯一解决方案涉及修改 XAML slightly

<StackPanel>
    <Label x:Name="lblSearch" Target="{Binding ElementName=txtSearch}" Content="_Search:" />
    <TextBox x:Name="txtSearch" AutomationProperties.LabeledBy="{Binding ElementName=lblSearch}"/>
</StackPanel>

通过执行此操作,您可以通过在文本框上调用 GetLabeledBy 来检索文本框的标签:

var labeledBy = AutomationProperties.GetLabeledBy(txtSearch);
Assert(labeledBy == lblSearch);

Apart from the solution by Josh Einstein, which is, if I'm not mistaken, equivalent to simply calling the static method AutomationProperties.GetLabeledBy, the only solution I see to this problem involves modifying the XAML slightly:

<StackPanel>
    <Label x:Name="lblSearch" Target="{Binding ElementName=txtSearch}" Content="_Search:" />
    <TextBox x:Name="txtSearch" AutomationProperties.LabeledBy="{Binding ElementName=lblSearch}"/>
</StackPanel>

By doing this, you can retrieve the label for the textbox by calling GetLabeledBy on the textbox:

var labeledBy = AutomationProperties.GetLabeledBy(txtSearch);
Assert(labeledBy == lblSearch);
梨涡少年 2024-08-26 02:58:30

按照 Aviad 回复评论中的逻辑,您可以创建自己的附加属性(我会为 Label 创建它),并且当您设置属性时,它应该在标签上设置 Target 并在元素上设置 AutomationProperties.GetLabeledBy。

如果您有多个标签,则只需对其中一个标签执行此操作,然后只需在其余标签上设置 Label.Target 即可。

Following the logic in the comment of Aviad's reply you could make your own attached property (I would make it for Label) and when you set the property it should set the Target on the label and AutomationProperties.GetLabeledBy on the element.

And if you have several labels only do this with one of them and simply set Label.Target on the rest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文