如何解决模板内儿童的 AutomatioID
我正在尝试读取位于容器内的自动化 ID(此处为模板)。
<Style x:Key="ContainerStyle" targettype="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander
Name="myExpander"
Margin="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"
FontFamily="Segoe UI"
Text="{Binding Path=ItemCount,Mode=OneWay}" />
<TextBlock
AutomationProperties.LiveSetting="Assertive"
AutomationProperties.AutomationId="Id2_notVisible"
Margin="5,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>
到目前为止我尝试过的: 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);
}
}
我读到位于模板(任何模板)内的 TextBlock 不可见。这就是我必须覆盖 OnCreateAutomationPeer() 方法的方式。但似乎没有任何效果。 我究竟做错了什么?设置自动化Id应该是一个简单的问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。