我有以下 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 中做任何额外的事情,除了您已经在此处看到的内容。
有任何想法吗?