2016-03-22 4 views
1

私は自分の問題についてのトリックまたは解決策を探しています。多分、あなたのうちの一人が何らかのアドバイスをしてくれますか?コンボボックスが開いている場合にのみ複雑なアイテムの説明を表示する方法WPF

STRUCTURE

は、だから私は以下のようにコードで私のコンボボックスがあります。

 <GridViewColumn Header="{lex:LocText CurrentWordCode}" controls:CustomColumnInfo.Name="CurrentWordCode"> 
      <GridViewColumn.CellTemplate> 
       <DataTemplate> 
        <ComboBox x:Name="ToolsComboBox" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,0,0" 
           SelectedItem="{Binding SelectedCurrentWord}" ItemsSource="{Binding AvailbleWords}" DisplayMemberPath="Item1" /> 

       </DataTemplate> 
      </GridViewColumn.CellTemplate> 
     </GridViewColumn> 

     <GridViewColumn Header="{lex:LocText CurrentWordName}" DisplayMemberBinding="{Binding SelectedCurrentWord.Item2}" 
         controls:CustomColumnInfo.Name="CurrentWordName"/> 

その後SelectedCurrentWordは

private Tuple<string,string, string> _SelectedCurrentWord; 
    public Tuple<string,string, string> SelectedCurrentWord 
    { 
     get { return _SelectedCurrentWord; } 
     set { _SelectedCurrentWord = value; 
      OnPropertyChanged("SelectedCurrentWord"); 
     } 
    } 

であり、私はこのような上記のオブジェクトを作成しています:

SelectedCurrentWord = new Tuple<string, string, string>(CurrentWordCode, CurrentWordName, CurrentWordCode + " - " + CurrentWordName) 

そしてItemSourceは、このようなものです:今、画像の最初の列はコード(タプルの最初の文字列)と第二が含まれてい

enter image description here

:RIGHT NOW

AvailbleWords = new List<Tuple<string, string, string>>(); 

列には名前(タプルの2番目の文字列)が含まれます。私の問題は、コンボボックスからアイテムを選択しているユーザーは、コードだけが表示されているのでアイテムが選択されていることを認識していませんが、アイテムの名前は認識していません。

PURPOSE

enter image description here

私の目的はCurrentWordCodeの列、ユーザーがコードを見ている、とCurrentWordNameで列ユーザがこの項目の名前を見ているということです。これは今のところ行われます。しかし、ユーザーがcomboxをクリックすると、 "Code" + " - " + "Name"のような形式でアイテムを表示したいと思います。

そのため、私はタプル構造の3番目のアイテムを作成しましたが、 "コード" + " - " + "名前"ですが、問題はコンボボックスのアイテムを選択してコンボボックスを開き、コンボボックスもこの形式ですが、ただ "コード"が必要です。

申し訳ありませんが、私が探しているものが明確でない場合は、質問してください。助けを求めるアドバイスをありがとう。よろしくお願いします。

+1

[本](http://stackoverflow.com/questions/8279107/different-template-for-items-in-comboboxs-drop-down-list-and-の重複のように見えます選択されたアイテム)。テンプレートセレクターで簡単に解決できます。また、ドロップダウンリスト項目も*美しくすることができます。 – Sinatr

答えて

1

まず、あなたがあなたのタプルで3番目の項目を失うことができ、これは表示puposesのための純粋で、すでに我々以来、存在してはなりません我々は他の2つの項目で必要なすべての情報を持っています。

第2に、コンボボックスにコンテンツの表示方法を伝えるためにcontroltemplateを使用する必要があります。実際には2つ必要です。選択した項目に1つ、ドロップダウンに1つ次に、データテンプレートを使用して、コンボボックスが両方のケースを処理することを確認できます。

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
      <ControlTemplate x:Key="SimpleTemplate"> 
       <StackPanel> 
        <TextBlock Text="{Binding Item1}" /> 
       </StackPanel> 
      </ControlTemplate> 
      <ControlTemplate x:Key="ExtendedTemplate"> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock Text="{Binding Item1}" /> 
        <TextBlock Text="-" /> 
        <TextBlock Text="{Binding Item2}" /> 
       </StackPanel> 
      </ControlTemplate> 
      <DataTemplate x:Key="TupleTemplate"> 
       <Control x:Name="theControl" Focusable="False" Template="{StaticResource ExtendedTemplate}" /> 
       <DataTemplate.Triggers> 
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ComboBoxItem}}, Path=IsSelected}" Value="{x:Null}"> 
         <Setter TargetName="theControl" Property="Template" Value="{StaticResource SimpleTemplate}" /> 
        </DataTrigger> 
       </DataTemplate.Triggers> 
      </DataTemplate> 
    </Window.Resources> 

<StackPanel> 
    <ComboBox x:Name="ToolsComboBox" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,0,0" Width="200" 
          SelectedItem="{Binding SelectedCurrentWord}" ItemsSource="{Binding AvailbleWords}" ItemTemplate="{StaticResource TupleTemplate}" /> 
</StackPanel> 

+0

ありがとうBjorn。コードは正常に動作しています。あなたのために本当にすべての良いもの! –

0

コンボボックスの以下のプロパティを設定する必要があるように見える:

<ComboBox x:Name="ToolsComboBox" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,0,0" SelectedItem="{Binding SelectedCurrentWord}" ItemsSource="{Binding AvailbleWords}" SelectedValuePath="Item1" SelectionBoxItemTemplate="{Path_to your template}" /> 
関連する問題