2011-11-05 50 views
1

我得到了一個ListBoxItem的一個DataTemplate,我想創建一個TRIGER,所以當用戶點擊一個項目的背景將改變,並在標籤列表框,DataTemplate中和觸發器

我的代碼:

<Window.Resources> 
    <Style x:Key="RoundedItem" TargetType="ListBoxItem"> 
     <EventSetter Event="MouseDoubleClick" Handler="listViewItem_MouseDoubleClick" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ListBoxItem"> 
        <Border Name="ItemBorder" CornerRadius="10" BorderBrush="Black" BorderThickness="1" Margin="1" Background="Transparent"> 
         <Label Name="ItemLabel" Foreground="Red" > 
          <ContentPresenter /> 
         </Label> 
        </Border> 

         <ControlTemplate.Triggers> 
         <Trigger Property="IsSelected" Value="True"> 
          <Setter TargetName="ItemBorder" Property="Background" Value="DeepSkyBlue" /> 
          <Setter TargetName="ItemLabel" Property="Foreground" Value="Orange" /> 
         </Trigger> 

        </ControlTemplate.Triggers> 
       </ControlTemplate> 

      </Setter.Value> 
     </Setter> 
    </Style> 

    <DataTemplate x:Key="TitleTemplate" DataType="models:Title" > 
     <StackPanel> 
       <Image Source="{Binding ThumbFilePath}" Width="50" HorizontalAlignment="Center"/> 
      <Label Content="{Binding Name}" HorizontalAlignment="Center" /> 
      <TextBlock Text="{Binding Description}" HorizontalAlignment="Center" TextWrapping="Wrap" Padding="5,5,5,5"/> 
     </StackPanel> 
    </DataTemplate> 

</Window.Resources> 

發生了什麼是TextBlock的改變他的顏色和n ot標籤..

有人知道爲什麼嗎? 謝謝。

回答

4

TextBlock從其父項在視覺樹中繼承前景定義。另一方面,Label以其默認樣式定義前景。

您的方法是「非WPF類型」 - 您不應將ContentPresenter包裝在Label控件中。

正確的做法取決於您是否想所有項目中的文本改變其前景,或只是標籤?

[在這兩種情況下,沒有明顯的好處在數據模板使用Label - 所以我會假設標籤更改爲TextBlock]

如果回答上述問題是所有的文字應改爲:在ListBoxItemControlTemplate,在觸發IsSelected,從seccond二傳手刪除TargetName="ItemLabel"所以最終制定者是:

<Setter Property="Foreground" Value="Orange" /> 

這將改變的前景該項目將影響數據模板中TextBlock的前景。

如果你想隻影響的TextBlocks之一:

1. remove the setter for the foreground from the control template 
2. add a trigger to your data template: 

<DataTemplate> 
    <StackPanel> 
     <Image .../> 
     <TextBlock x:Name="Text01" ..../> 
     <TextBlock x:Name="Text02" ..../> 
    </StackPanel> 
    <DataTemplate.Triggers> 
     <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="True"> 
      <Setter TargetName="Text01" Property="Foreground" Value="Orange"/> 
     </DataTrigger> 
    </DataTemplate.Triggers> 
</DataTemplate> 

附註:如果您在數據模板中使用Label控制,約束其前景屬性列表框的前景項,像這樣:

<Label Foreground="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"....../> 

如果這沒有幫助,就意味着你的列表框項目繼承其前景,所以使用:

<Label Foreground="{Binding TextElement.Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"....../> 
+0

謝謝,很好的答案! – ibm123

1

我想解決這個問題,我遇到了類似的問題,我在ListBox中添加了一個ListBox.ItemTemplate,然後樣式不再適用於文本。

我在做的是試圖顯示一個語言列表(CultureInfo)供用戶選擇,但我希望顯示本機名稱,而不是英文名稱。由於某些原因,並非所有的語言都有在CultureInfo中大寫的本地名,並且NativeName是它們名稱的唯一實例,所以我需要將一個函數應用於CultureInfo。NativeName將我自己的名字大寫。爲了做到這一點,我添加了一個DataTemplate,裏面有一個Data Template,我在其上應用了一個轉換器。

<ListBox IsSynchronizedWithCurrentItem="True" VerticalAlignment="Center" MinHeight="200" x:Name="cbLanguages" 
    ItemsSource="{Binding Path=SupportedCultures, Mode=OneWay, Source={StaticResource CultureResourcesDS}}" 
    FontSize="24" HorizontalAlignment="Stretch" Width="300" Margin="10" 
    Style="{DynamicResource ListBoxTemplate}" ItemContainerStyle="{DynamicResource ListBoxItemStyle}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Label Content="{Binding Converter={StaticResource NativeNameConverter}}"/> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

一陣搜索後,我碰到XAMeLi的答案在這裏,和改變了標籤我放在DataTemplate中到一個文本框,我會創造了ListBoxItemStyle再次合作。

基本上,標籤和文本框有不同的特點,可以被利用,或可能會導致在這種情況下惱人的問題。這裏有一個很好的解釋與差異的一些例子:http://joshsmithonwpf.wordpress.com/2007/07/04/differences-between-label-and-textblock/

相關問題