2011-01-18 8 views
1

私は今、私はすべての変更を加えていない、ListBoxItemコントロールにControlTemplateのを設定しようとしている、それはです:ListBoxItemのControlTemplateとItemTemplateSelector

<ControlTemplate TargetType="ListBoxItem" 
       x:Key="listBoxItemCustomTemplate"> 
    <Border BorderThickness="{TemplateBinding Border.BorderThickness}" 
      Padding="{TemplateBinding Control.Padding}" 
      BorderBrush="{TemplateBinding Border.BorderBrush}" 
      Background="{TemplateBinding Panel.Background}" 
      Name="Bd" 
      SnapsToDevicePixels="True"> 
     <ContentPresenter Content="{TemplateBinding ContentControl.Content}" 
          ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" 
          HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" 
          VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" 
          SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" /> 
    </Border> 
    <ControlTemplate.Triggers> 
     <Trigger Property="Selector.IsSelected"> 
      <Setter Property="Panel.Background" TargetName="Bd"> 
       <Setter.Value> 
        <DynamicResource ResourceKey="{x:Static SystemColors.HighlightBrushKey}" /> 
       </Setter.Value> 
      </Setter> 
      <Setter Property="TextElement.Foreground"> 
       <Setter.Value> 
        <DynamicResource ResourceKey="{x:Static SystemColors.HighlightTextBrushKey}" /> 
       </Setter.Value> 
      </Setter> 
      <Trigger.Value> 
       <s:Boolean>True</s:Boolean> 
      </Trigger.Value> 
     </Trigger> 
     <MultiTrigger> 
      <MultiTrigger.Conditions> 
       <Condition Property="Selector.IsSelected"> 
        <Condition.Value> 
         <s:Boolean>True</s:Boolean> 
        </Condition.Value> 
       </Condition> 
       <Condition Property="Selector.IsSelectionActive"> 
        <Condition.Value> 
         <s:Boolean>False</s:Boolean> 
        </Condition.Value> 
       </Condition> 
      </MultiTrigger.Conditions> 
      <Setter Property="Panel.Background" TargetName="Bd"> 
       <Setter.Value> 
        <DynamicResource ResourceKey="{x:Static SystemColors.ControlBrushKey}" /> 
       </Setter.Value> 
      </Setter> 
      <Setter Property="TextElement.Foreground"> 
       <Setter.Value> 
        <DynamicResource ResourceKey="{x:Static SystemColors.ControlTextBrushKey}" /> 
       </Setter.Value> 
      </Setter> 
     </MultiTrigger> 
     <Trigger Property="UIElement.IsEnabled"> 
      <Setter Property="TextElement.Foreground"> 
       <Setter.Value> 
        <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" /> 
       </Setter.Value> 
      </Setter> 
      <Trigger.Value> 
       <s:Boolean>False</s:Boolean> 
      </Trigger.Value> 
     </Trigger> 
    </ControlTemplate.Triggers> 
</ControlTemplate> 

これは正常に動作し、問題は、ListBoxでItemTemplateSelectorを使用しようとするときです。 DataTemplateSelectorコードは実行されません。明らかに、そのControlTemplateでItemTemplateSelectorが機能しないようなことがありますが、どうしたらよいか分かりません。

<ListBox x:Name="listBox" 
     ItemsSource="{Binding AllItems}" 
     ItemTemplateSelector="{DynamicResource ExperimentExplorerTemplateSelector}" 
     ItemContainerStyle="{DynamicResource customListBoxItemStyle}" /> 

そしてControlTempalteセットスタイル:なぜこれが起こっている

<Style TargetType="{x:Type ListBoxItem}" x:Key="customListBoxItemStyle"> 
    <Setter Property="Template" Value="{StaticResource listBoxItemCustomTemplate}" /> 
</Style> 

任意のアイデアを

これはListBoxのでしょうか?

ありがとうございました。

答えて

3

ShowMeTheTemplateを使用したようですが、これが正しく動作するとは限りません。ここで

があるShowMeTheTemplate(エアロ)から ListBoxItemテンプレートの ContentPresenter

<ContentPresenter 
    Content="{TemplateBinding ContentControl.Content}" 
    ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" 
    ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" 
    HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" 
    VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" 
    SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" /> 

とブレンドから同じセクション:

<ContentPresenter 
    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 

と他の違いがそこにもあるが、特にこのライン:

ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" 

あなたの原因は何ですかDataTemplateSelectorがバイパスされます。 ContentStringFormatは、TemplateSelectorが設定されていると無視されるはずなので、理由を理解できるとは言えません。

Blendの抽出と同じテンプレート/スタイルを抽出するために、より信頼性の高い方法が必要であるということです。

+0

nice!それは働いた、私はそれを把握しようといくつかの良い3〜4を過ごした。ありがとうございました! – Carlo

関連する問題