我想将AutomationProperties.Name
绑定到包含在secondTextBox
中的文本怎么做?仅使用没有代码的xaml。
的Xaml:
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication5"
xmlns:converters="clr-namespace:WpfApplication5.Converters"
mc:Ignorable="d"
Title="TestWindow" Height="350" Width="525">
<Window.Resources>
<converters:StrangeConverter x:Key="CommonConverter"/>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<ComboBox Height="20" Width="100" ItemsSource="{Binding comboBoxItems}" AutomationProperties.AutomationId="ID_COMBO1">
<ComboBox.ItemContainerStyle>
<Style>
<!--<Setter Property="AutomationProperties.Name" Value="{Binding UniqNumber}"/>-->
<Setter Property="AutomationProperties.Name" Value="{Binding Path=Text, ElementName=secondTextBox}"/>
</Style>
</ComboBox.ItemContainerStyle>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="secondTextBox">
<Run Text="{Binding Name}"></Run>
<Run Text="-"></Run>
<Run Text="{Binding UniqNumber}"></Run>
<Run Text="{Binding .,Converter={StaticResource CommonConverter}}"></Run>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
<ComboBox Height="20" Width="100" ItemsSource="{Binding comboBoxItems}" AutomationProperties.AutomationId="ID_COMBO2"/>
<Button Grid.Row="1" Height="20" Width="100" Click="Button_Click"/>
</Grid>
</Window>
转换器:
namespace WpfApplication5.Converters
{
class StrangeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return "Test";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
代码:
namespace WpfApplication5
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ViewModel Vm = new ViewModel();
public MainWindow()
{
InitializeComponent();
DataContext = Vm;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var _calculatorAutomationElement = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "TestWindow"));
var combobox = _calculatorAutomationElement.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.AutomationIdProperty, "ID_COMBO1"));
Vm.SelectComboboxItem(combobox, "02 - Basic Get");
combobox = _calculatorAutomationElement.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.AutomationIdProperty, "ID_COMBO2"));
Vm.SelectComboboxItem(combobox, "01 - Basic Set");
}
}
public class ViewModel
{
public List<Item> comboBoxItems { get; set; }
public ViewModel()
{
comboBoxItems = new List<Item>();
comboBoxItems.Add(new Item { Name = "Basic Get", UniqNumber = 1 });
comboBoxItems.Add(new Item { Name = "Basic Set", UniqNumber = 2 });
comboBoxItems.Add(new Item { Name = "Basic Report", UniqNumber = 3 });
}
public bool SelectComboboxItem(AutomationElement comboBox, string item)
{
(comboBox.GetCurrentPattern(ExpandCollapsePattern.Pattern) as ExpandCollapsePattern).Expand();
PropertyCondition findCondition = new PropertyCondition(AutomationElement.NameProperty, item);
var comboBoxItems = comboBox.FindFirst(TreeScope.Children, findCondition);
if (comboBoxItems != null)
{
var selectionItemPattern = comboBoxItems.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
selectionItemPattern.Select();
return true;
}
return false;
}
}
public class Item
{
public string Name { get; set; }
public int UniqNumber { get; set; }
}
}
答案 0 :(得分:0)
将Item
课程更新为:
public class Item
{
private string _TextToDisplay;
public string Name { get; set; }
public int UniqNumber { get; set; }
public string TextToDisplay
{
get
{
_TextToDisplay = Name + "-" + UniqNumber; //Add other modification from converter
return _TextToDisplay;
}
set
{
_TextToDisplay = value;
}
}
}
将AutomationProperties.Name
绑定到此属性
<TextBlock x:Name="secondTextBox" Text="{Binding TextToDisplay}" AutomationProperties.Name="{Binding TextToDisplay}">
答案 1 :(得分:0)
这应该没有额外的视图模型属性且没有转换器:
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="AutomationProperties.Name">
<Setter.Value>
<MultiBinding StringFormat="{}{0} - {1} Test">
<Binding Path="Name"/>
<Binding Path="UniqNumber"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</ComboBox.ItemContainerStyle>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1} Test">
<Binding Path="Name"/>
<Binding Path="UniqNumber"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>