Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

wpf - AutomatioID for children inside a template

I'm trying to read Automation ID's located inside container (here a template).

<Style x:Key="ContainerStyle" TargetType="{x:Type GroupItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>                        
                    <Expander
                        Name="myExpander"                            
                        Margin="0,0,-2,0"
                        Loaded="Expander_Loaded">
                        <Expander.Header>
                            <Grid
                                Name="grid"
                                HorizontalAlignment="Stretch"
                                MouseDown="Grid_MouseDown">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Grid.ContextMenu>
                                    <ContextMenu >
                                        <MenuItem
                                            Click="RenameGroup_Click"
                                            FontFamily="Segoe UI"
                                            Header="Rename Group">
                                            
                                        </MenuItem>
                                        <MenuItem
                                            Click="DeleteGroup_Click"
                                            FontFamily="Segoe UI"
                                            Header="Delete Group">
                                            
                                        </MenuItem>
                                    </ContextMenu>
                                </Grid.ContextMenu>                                   
                                <local:AutomatisableTextBlock                                        
                                    AutomationProperties.AutomationId="Id1_notVisible"
                                    HorizontalAlignment="Left"
                                    FontFamily="Segoe UI"
                                    FontWeight="Bold"                                        
                                    Text="{Binding Name}" />
                                <StackPanel
                                    Grid.Column="1"
                                    HorizontalAlignment="Right"
                                    Orientation="Horizontal"
                                    TextBlock.FontFamily="Segoe UI">
                                    <TextBlock                                                                                       
                                        AutomationProperties.AutomationId="{Binding Name, UpdateSourceTrigger=PropertyChanged}"
                                        Margin="5,0,0,0"
                                        FontFamily="Segoe UI"                                            
                                        Text="{Binding Path=ItemCount, Mode=OneWay}" />
                                    <TextBlock
                                        AutomationProperties.LiveSetting="Assertive"
                                        AutomationProperties.AutomationId="Id2_notVisible"                                            
                                        Margin="5,0,0,0"
                                        FontFamily="Segoe UI">
                                    Test(s)
                                    </TextBlock>

                                </StackPanel>
                            </Grid>
                        </Expander.Header>
                        <ItemsPresenter />
                    </Expander>
                </ControlTemplate>                    
            </Setter.Value>
        </Setter>
        <!--<Setter Property="AutomationProperties.AutomationId" Value="Id3_Visible"/>-->
    </Style>

What I have tried until now: 1.

/// <summary>
/// to be.
/// </summary>
public class AlwaysVisibleTextBlockAutomationPeer : TextBlockAutomationPeer
{
    /// <summary>
    /// Initializes a new instance of the <see cref="AlwaysVisibleTextBlockAutomationPeer"/> class.
    /// </summary>
    /// <param name="t">tee.</param>
    public AlwaysVisibleTextBlockAutomationPeer(TextBlock t)
    : base(t)
    {
    }

    /// <summary>
    /// sfs.
    /// </summary>
    /// <returns>sdf.</returns>
    protected override string GetClassNameCore()
    {
        return "TextBlock";
    }

    /// <inheritdoc/>
    protected override bool IsControlElementCore()
    {
        // if (String.IsNullOrEmpty(GetAutomationId()))
        // return false;
        return true;
    }

    /// <summary>
    /// jhds.
    /// </summary>
    /// <returns>dfgdg.</returns>
    protected override bool IsContentElementCore()
    {
        return true;
    }

    /// <summary>
    /// sffs.
    /// </summary>
    /// <returns>sfsf.</returns>
    protected override AutomationControlType GetAutomationControlTypeCore()
    {
        return AutomationControlType.Text;
    }
}



  public class AutomatisableTextBlock : TextBlock
{
    /// <summary>
    /// tr.
    /// </summary>
    /// <returns>gg.</returns>
    protected override AutomationPeer OnCreateAutomationPeer()
    {
        return new AlwaysVisibleTextBlockAutomationPeer(this);
    }
}
private void Expander_Loaded(object sender, RoutedEventArgs e)
    {
        // add
        var expander = sender as Expander;
        expander.IsExpanded = true;
        expander.UpdateLayout();
        var textBlock = this.FindTextBlock(expander, "Id2_notVisible");

        if (textBlock != null)
        {               
            var peer = UIElementAutomationPeer.FromElement(textBlock);
            if (peer == null)
            {
                peer = UIElementAutomationPeer.CreatePeerForElement(textBlock);
            }

            peer.RaiseAutomationEvent(AutomationEvents.PropertyChanged);
        }
    }

I read that TextBlock located inside a template (any template) is not visible. That's way I have to override OnCreateAutomationPeer() method. But nothing seems to work. What am I doing wrong? It supposed to be a simple problem to set an automation Id.

question from:https://stackoverflow.com/questions/66050100/automatioid-for-children-inside-a-template

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...