Add Automation ID to bound combo box items

2020-07-27 04:11发布

问题:

I am not too familiar with XAML or binding, but I have been setting AutomationID's on each GUI element. Unfortunately I can't find a way to set an AutomationID on items in a ComboBox.

Here is how the ComboBox is declared in XAML.

<ComboBox AutomationProperties.AutomationId="DialogRODB_TypeComboBox"
          Height="23"
          Margin="80,64,27,0"
          VerticalAlignment="Top"
          SelectedValue="{Binding Message.Move.Type}"
          ItemsSource="{Binding Source={StaticResource MoveType}}" />

And in a separate class, this is where the items in the combo box are created.

public enum MoveType
{
    [StringValue("INBOUND")]    Inbound,
    [StringValue("OUTBOUND")]   Outbound
}

I can't really provide more code, but I can try to answer any questions.

回答1:

I think you need to define an ItemTemplate and than you can put Automationid to each element. For e.g.

<DataTemplate x:Key="PersonDataTemplate" DataType="model:Person">
    <TextBlock Text="{Binding Name}">
        <AutomationProperties.AutomationId>
            <MultiBinding StringFormat="AID_{0}-{1}">
                <Binding Path="Name" />
                <Binding Path="Id" />
            </MultiBinding >
        </AutomationProperties.AutomationId>
    </TextBlock>
</DataTemplate>