简体   繁体   English

在WPF中以编程方式将AutomationId分配给模板控件

[英]Programatically Assigning AutomationIds to Templated Controls in WPF

I've got a control template that looks like: 我有一个控件模板,如下所示:

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

I've been able to create a setter tag to assign an automationid to the instantiated objects. 我已经能够创建一个setter标记,为实例化的对象分配一个Automationid。 But as one would expect all of the controls have the same ID. 但正如人们期望的那样,所有控件都具有相同的ID。

There's some further XAML that defines 3 thumbs (center, left, and right), I'm wanting to define their IDs in such a way that they would like: 还有一些进一步的XAML定义了3个大拇指(中,左,右),我想用他们想要的方式来定义它们的ID:

Center_1 Right_1 Left_1 Center_1右_1左_1

Center_2 Right_2 Left_2 中心_2右_2左_2

Any ideas on how I can make this happen? 关于如何实现此目标的任何想法?

I'm guessing I would need to have a static member variable to track the number of instantiated objects (and then use that to increment the name value), but beyond that I'm at a loss. 我猜想我需要一个静态成员变量来跟踪实例化对象的数量(然后使用它来增加名称值),但是除此之外,我很茫然。

Thanks in advance! 提前致谢!

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM