c# - 以编程方式将 AutomationIds 分配给 WPF 中的模板化控件

标签 c# wpf xaml microsoft-ui-automation

我有一个如下所示的控件模板:

<Style TargetType="Thumb">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Ellipse Fill="{TemplateBinding Background}"></Ellipse>
            </ControlTemplate>
        </Setter.Value>
    <Setter>
</Style>

我已经能够创建一个 setter 标签来为实例化的对象分配一个 automationid。但正如人们所期望的那样,所有控件都具有相同的 ID。

还有一些进一步的 XAML 定义了 3 个拇指(中心、左和右),我想以他们喜欢的方式定义他们的 ID:

中心_1 右_1 左_1

中心_2 右_2 左_2

关于如何实现这一点有什么想法吗?

我猜我需要有一个静态成员变量来跟踪实例化对象的数量(然后使用它来增加名称值),但除此之外我不知所措。

提前致谢!

最佳答案

Uid property 应该是你需要设置的属性。

关于c# - 以编程方式将 AutomationIds 分配给 WPF 中的模板化控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31968504/

相关文章:

c# - 从 Web API 中的 Controller 返回由 OAuthAuthorizatioServer 生成的 JWT token

c# - PrintDialog/XPS Document Writer 中忽略的纸张大小

xaml - Visual Studio 2012 是否支持 XAML 2009?

c# - 如何使 Richtext block 可滚动?

c# - 将基于 mfc 对话框的 Windows 应用程序转换为基于 Web

c# - 将 IEnumerable<class> 的结果绑定(bind)到组合框

c# - Visual Studio Code Ubuntu - 标记输入时模式失败

c# - 分块到 ASP.Net Web API 的多个异步文件上传

c# - C# for 循环中的两个计数器

c# - Wpf 中的图像控件显示已删除的上一个图像