2017-08-10 12 views
0

を含むテキストにsecondTextBoxをバインドしますか? xamlのみをコードなしで使用する。ItemTemplateのDataTemplateのItemContainerStyleのWpfバインドプロパティ

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

あなたは 'Item'クラスでTextToDisplay''のようなプロパティを作成して計算する必要がありますそこにはxamlの代わりにそれがあります。そして、あなたはこのプロパティに 'AutomationProperties.Name'をバインドできます。 –

+0

残念ですが、私はこれをxamlで行う必要があります。 – A191919

+0

私の答えをチェックしてください。それはXAML –

答えて

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

しかし、タグバインディングでMultiBindingでコンバータを使用するには?この場合、私は新しいIMultiConverterを実装する必要があります。 – A191919

+0

マルチバインディングのConverterプロパティを、IMultiValueConverterを実装するクラスのインスタンスに設定します。ここには必要ないとは思われるが... – Clemens

関連する問題