XAML - Automation ID for ComboBox elements in Data

2019-05-28 17:16发布

问题:

To start off, the method that I'm using for automating individual elements of ComboBox is as follows:

<ComboBox.ItemContainerStyle>
    <Style TargetType="ComboBoxItem">
        <Setter Property="AutomationProperties.AutomationId"
                Value="{Binding ProviderName}" />
    </Style>
</ComboBox.ItemContainerStyle>

ProviderName is bound to the ComboBox's DisplayMemberPath property.

My problem is that ItemContainerStyle does not seem to be available when trying to modify DataGridComboBoxColumn so that the elements of the combo box contained within also have automation IDs.

Has anyone come across a method for doing so?

I'm still toying around with different ideas, so if I come across any meaningful results I'll post them. Thanks in advance.

回答1:

After some experimenting, I managed to get it to work by setting the EditingElementStyle.

Here's the XAML:

<DataGridComboBoxColumn.EditingElementStyle>
    <Style TargetType="{x:Type ComboBox}">
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="ComboBoxItem">
                    <Setter Property="AutomationProperties.AutomationId"
                            Value="{Binding ProviderName}" />
                </Style>
            </Setter.Value>
        </Setter>
    </Style>
</DataGridComboBoxColumn.EditingElementStyle>