ItemContainerStyle 中的 wpf 将属性绑定到 ItemTemplate 的数据模板中的 TextBl



我想将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; }
}
}

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}">

这应该在没有其他视图模型属性和转换器的情况下工作:

<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>

最新更新