2011-11-04 14 views
9

私はサイト上でいくつかの質問をしましたが、答えが見つからないようです。ComboBoxのカスタムテンプレートは、DisplayMemberPathの使用を無視します

私はComboBoxを持っています。それはうまく働いていた。私は外観をオーバーホールする必要があると判断したので、私は(これはストレートコピー、変更なしで)、デフォルトのコンボボックスのテンプレートのコピーを作成します。私はある(私のリストから項目を選択すると、今

<ControlTemplate x:Key="ComboBoxControlTemplate2" TargetType="{x:Type ComboBox}"> 
      <Grid x:Name="MainGrid" SnapsToDevicePixels="True"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="*"/> 
        <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/> 
       </Grid.ColumnDefinitions> 
       <Popup x:Name="PART_Popup" AllowsTransparency="True" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom"> 
        <Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=MainGrid}"> 
         <Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"> 
          <ScrollViewer x:Name="DropDownScrollViewer"> 
           <Grid RenderOptions.ClearTypeHint="Enabled"> 
            <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0"> 
             <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=DropDownBorder}" Height="{Binding ActualHeight, ElementName=DropDownBorder}" Width="{Binding ActualWidth, ElementName=DropDownBorder}"/> 
            </Canvas> 
            <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
           </Grid> 
          </ScrollViewer> 
         </Border> 
        </Microsoft_Windows_Themes:SystemDropShadowChrome> 
       </Popup> 
       <ToggleButton BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"> 
        <ToggleButton.Style> 
         <Style TargetType="{x:Type ToggleButton}"> 
          <Setter Property="OverridesDefaultStyle" Value="True"/> 
          <Setter Property="IsTabStop" Value="False"/> 
          <Setter Property="Focusable" Value="False"/> 
          <Setter Property="ClickMode" Value="Press"/> 
          <Setter Property="Template"> 
           <Setter.Value> 
            <ControlTemplate TargetType="{x:Type ToggleButton}"> 
             <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" SnapsToDevicePixels="True"> 
              <Grid HorizontalAlignment="Right" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"> 
               <Path x:Name="Arrow" Data="M0,0L3.5,4 7,0z" Fill="Black" HorizontalAlignment="Center" Margin="3,1,0,0" VerticalAlignment="Center"/> 
              </Grid> 
             </Microsoft_Windows_Themes:ButtonChrome> 
             <ControlTemplate.Triggers> 
              <Trigger Property="IsChecked" Value="True"> 
               <Setter Property="RenderPressed" TargetName="Chrome" Value="True"/> 
              </Trigger> 
              <Trigger Property="IsEnabled" Value="False"> 
               <Setter Property="Fill" TargetName="Arrow" Value="#FFAFAFAF"/> 
              </Trigger> 
             </ControlTemplate.Triggers> 
            </ControlTemplate> 
           </Setter.Value> 
          </Setter> 
         </Style> 
        </ToggleButton.Style> 
       </ToggleButton> 
       <ContentPresenter ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="False" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
      </Grid> 
      <ControlTemplate.Triggers> 
       <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="True"> 
        <Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/> 
        <Setter Property="Color" TargetName="Shdw" Value="#71000000"/> 
       </Trigger> 
       <Trigger Property="HasItems" Value="False"> 
        <Setter Property="Height" TargetName="DropDownBorder" Value="95"/> 
       </Trigger> 
       <Trigger Property="IsEnabled" Value="False"> 
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
        <Setter Property="Background" Value="#FFF4F4F4"/> 
       </Trigger> 
       <Trigger Property="IsGrouping" Value="True"> 
        <Setter Property="ScrollViewer.CanContentScroll" Value="False"/> 
       </Trigger> 
       <Trigger Property="CanContentScroll" SourceName="DropDownScrollViewer" Value="False"> 
        <Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}"/> 
        <Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}"/> 
       </Trigger> 
      </ControlTemplate.Triggers> 
     </ControlTemplate> 

POCOのコレクション)、それは想定されていた値の代わりに名前空間とクラス名を表示します。

私の研究と実験により、私の新しいテンプレートではDisplayMemberPathのプロパティを使用しないという問題があると私は考えました。 OnDisplayMemberPathChangedメソッドをオーバーライドしてItemTemplateを設定しようとしましたが、リストから項目を選択するとエラーが発生します。

私はXAML経由で人々がItemTemplateを設定しているのを見たことがありますが、私は何百ものコンボボックスを持っています。

ControlTemplateのDisplayMemberPathプロパティを使用する方法がいくつかありますか、欲しい結果を得るために派生したコントロールで実行できるコードがありますか?

答えて

19

は、1つの重要な事は、この要素に欠けている:

<ContentPresenter ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
     Content="{TemplateBinding SelectionBoxItem}" 
     ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" 
     HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
     IsHitTestVisible="False" 
     Margin="{TemplateBinding Padding}" 
     SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
     VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> 

たぶん、あなたが誤って削除された、すなわち:

ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" 

あなたはこれを持っていない場合ComboBoxは、テンプレートセレクタ(ItemTemplateまたはDisplayMemberPathを使用できるように)を使用してテンプレートの中から選択するため、DisplayMemberPathを設定しないでください。

+0

ちょっと!それは完全に働いた。私は決してその解決策を見たことはありません、そして、私はそれが決して生成されなかった100%肯定的です。私はこのプロセスを3〜4回やり、同じ問題を抱えている人々のスコアをチェックしました。これは、Microsoft側のバグであるはずです...ありがとう! – CamronBute

+0

喜びが落ちたので、私はあなたがそれをどのように理解したのかを尋ねなければなりません。テンプレートコピーを生成するのにあなたは何を使用していますか? – CamronBute

+3

@CamronBute:まあ、私はいつでも[MSDN](http://msdn.microsoft.com/en-us/library/aa970773.aspx)( 'Default WPF Themes'リンク)で入手できるデフォルトのテンプレートを持っています私は物事がどのように縛られているか見るためにそれを調べました。この「ContentPresenter」が選択された項目のキーコンポーネントであることがわかりにくいので、エラーがある場合はそこにあるはずです。次に、このプロパティが見つからないことを確認するだけです。私はまた、自分自身でそのプロパティを削除する効果をテストし、期待通りにタイプ名のみを示しました。 –

1

私はsame questionがあったが、それは単にそれにその値をTextBlockItemTemplateを設定するためのショートカットの方法ですDisplayMemberPathを設定することが判明しました。

ItemTemplateを設定すると、DisplayMemberPathは、デフォルトのTextBlockに追加する値を上書きしてしまいますので、無用になります。 ない正確なコピーである

+0

上記の解答を確認してください。私は関連する質問であなたの問題を見ましたが、上記の解決策は私のために働く!とにかくおかげで、:) – CamronBute

+0

私はH.Bによって解決策を使用しました。しかし、この答えの結果、 'ContentTemplate =" {TemplateBinding ItemTemplate} "もうまくいきました。しかし、私は副作用をテストするのに十分長い間それを残していませんでした。 – OhBeWise

0

私は同じ問題を抱えましたが、これはバグでなければなりません。私はそれを報告しようとします。

は、Visual Studioからコントロールテンプレートの "コピーを編集"、それはラインミス:

ContentTemplateSelectorを= "{TemplateBindingのItemTemplateSelector}" 原因

"DisplayMemberPath" 正常に動作していません。

0

私は、これは古い質問ですけど、私は、これらの日に同じ問題に直面し、私はそれはあなたが欠落していないことを確認してください

ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"

代わりの

ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"

1

追加する固定しましたContentTemplateSelector要素。

COMBOXテンプレート自体のために、それは次のようになります。

ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" 

ItemsContainerStyleのために、それは次のようになります。

ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"   
関連する問題