DragDropItemsControl
の独自のAutomationPeer
を作成し、独自のコントロールアイテムを作成する必要がある場合、AutomationIdをアイテムオブジェクトの識別子として定義することができます。
public class DragDropItemsControl : ItemsControl
{
protected override AutomationPeer OnCreateAutomationPeer()
{
return new DragDropItemsAutomationPeer(this);
}
}
カスタムAutomationPeer
クラスです。
public class DragDropItemsControlAutomationPeer : ItemsControlAutomationPeer
{
public DragDropItemsControlAutomationPeer(DragDropItemsControl owner)
: base(owner)
{
}
protected override string GetClassNameCore()
{
return "DragDropItemsControl";
}
protected override ItemAutomationPeer CreateItemAutomationPeer(object item)
{
return new DragDropItemsControlItemAutomationPeer(item, this);
}
}
コントロールアイテムのカスタムAutomationPeer
クラスです。 ここで重要な部分は、メソッドGetAutomationIdCore()
の実装です。
public MyMainWindow()
{
InitializeComponent();
List<MyTestItemObject> items = new List<MyTestItemObject>();
items.Add(new MyTestItemObject() { Title = "Learning TestStack.White", ItemId="007" });
items.Add(new MyTestItemObject() { Title = "Improve my english", ItemId = "008" });
items.Add(new MyTestItemObject() { Title = "Work it out", ItemId = "009" });
icTodoList.ItemsSource = items;
}
public class MyTestItemObject
{
public string Title { get; set; }
public string ItemId { get; set; }
}
の背後にあるコードで、次のXAMLコード
<local:MyItemsControl x:Name="icTodoList" AutomationProperties.AutomationId="TestItemsControl">
<local:MyItemsControl.ItemTemplate>
<DataTemplate>
<Border >
<TextBlock Cursor="Hand" Text="{Binding Title}" />
</Border>
</DataTemplate>
</local:MyItemsControl.ItemTemplate>
</local:MyItemsControl>
初期化のために
public class DragDropItemsControlItemAutomationPeer : ItemAutomationPeer
{
public DragDropItemsControlItemAutomationPeer(object item, ItemsControlAutomationPeer itemsControlAutomationPeer)
: base(item, itemsControlAutomationPeer)
{
}
protected override string GetClassNameCore()
{
return "DragDropItemsControl_Item";
}
protected override string GetAutomationIdCore()
{
return (base.Item as MyTestItemObject)?.ItemId;
}
protected override AutomationControlType GetAutomationControlTypeCore()
{
return base.GetAutomationControlType();
}
}
我々は
サンプルUIAVerifyで見ることができます値を確認するコード
// retrieve the custom control
IUIItem theItemsControl = window.Get(SearchCriteria.ByAutomationId("008"));
if (theItemsControl is CustomUIItem)
{
// retrieve the custom control container
IUIItemContainer controlContainer = (theItemsControl as CustomUIItem).AsContainer();
// get the child components
WPFLabel theTextBlock = controlContainer.Get<WPFLabel>(SearchCriteria.Indexed(0));
// get the text value
string textValue = theTextBlock.Text;
}
セットの各アイテムに個々の名前/ AutomationIdを与える方法を尋ねていますか? – LordWilmore
@LordWilmoreはい。私はItemsControlから各項目を取得する解決策が見つかりませんでした –
したがって、何か一意にautomationproperties.automationidを設定する必要があります。したがって、適切な文字列を選択し、オブジェクト内で一意であるものにバインドするプレフィックスを追加します(例: ID。 – LordWilmore