2011-03-09 72 views
0

這裏是我的XAML:WPF中的ListBoxItem ControlTemplate設計問題?

<ListBox 
    Name="PlaylistListBox" 
    ScrollViewer.CanContentScroll="False" 
    HorizontalContentAlignment="Stretch" 
    ItemsSource="{Binding Source={StaticResource ResourceKey=MyListBoxView}}" 
    ItemTemplateSelector="{Binding Source={StaticResource ResourceKey=MySelector}}" 
    MouseDoubleClick="PlaylistListBox_MouseDoubleClick" > 
    <ListBox.ItemContainerStyle> 
    <Style TargetType="ListBoxItem"> 
     <Setter Property="SnapsToDevicePixels" Value="true"/> 
     <Setter Property="OverridesDefaultStyle" Value="true"/> 
     <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ListBoxItem"> 
      <Border 
       Name="Border" 
       CornerRadius="4" 
       SnapsToDevicePixels="true"> 
       <ContentPresenter /> 
      </Border> 
      <ControlTemplate.Triggers> 
       <Trigger Property="IsSelected" Value="true"> 
       <Setter TargetName="Border" Property="Background" Value="Black" /> 

       <!-- The following setter for opacity is giving me headaches --> 
       <Setter TargetName="Border" Property="Opacity" Value="0.5" /> 

       </Trigger> 
      </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
     </Setter> 
     <EventSetter 
     Event="Loaded" 
     Handler="PlaylistListBoxItem_Loaded" /> 
    </Style> 
    </ListBox.ItemContainerStyle> 
</ListBox> 

兩個問題:

  1. 因爲Opacity二傳手,整個項目的50%是透明的。我只想ListBoxItemControlTemplate中定義的 邊框爲透明,其內容爲 以保持完全不透明。
  2. ListBox不是 焦點時,如何使Setter/Trigger使相同邊框變爲紅色?它應該是像InFocus="False"IsSelected="True"

感謝您的澄清。

回答

2
  1. 您應該在內容下方放置另一個邊框,並使其半透明,使主內容完全可見。您可以通過使用Grid並在其中首先放置一個「背景」邊框,然後再放入內容來完成此操作。這樣你將只在背景邊框上設置不透明度,但不在內容上;

  2. 您可以使用MultiTrigger

下面是修改的風格,顯示了我的意思是:

<ListBox 
    Name="PlaylistListBox" 
    ScrollViewer.CanContentScroll="False" 
    HorizontalContentAlignment="Stretch" 
    ItemsSource="{Binding Source={StaticResource ResourceKey=MyListBoxView}}" 
    ItemTemplateSelector="{Binding Source={StaticResource ResourceKey=MySelector}}" 
    MouseDoubleClick="PlaylistListBox_MouseDoubleClick" > 
    <ListBox.ItemContainerStyle> 
    <Style TargetType="ListBoxItem"> 
     <Setter Property="SnapsToDevicePixels" Value="true"/> 
     <Setter Property="OverridesDefaultStyle" Value="true"/> 
     <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ListBoxItem"> 
      <Grid> 
       <Border 
       Name="BackgroundBorder" 
       CornerRadius="4" 
       SnapsToDevicePixels="true">     
       </Border> 
       <Border Name="Border"> 
       <ContentPresenter /> 
       </Border> 
      </Grid> 
      <ControlTemplate.Triggers> 
       <Trigger Property="IsSelected" Value="true"> 
       <Setter TargetName="BackgroundBorder" Property="Background" Value="Black" /> 

       <!-- The following setter for opacity is giving me headaches --> 
       <Setter TargetName="BackgroundBorder" Property="Opacity" Value="0.5" /> 

       </Trigger> 
       <MultiTrigger> 
       <MultiTrigger.Conditions> 
         <Condition Property="IsSelected" Value="True"/> 
         <Condition Property="IsFocused" Value="False"/> 
       </MultiTrigger.Conditions> 
       <Setter TargetName="BackgroundBorder" Property="Background" Value="Red" /> 
       </MultiTrigger> 
      </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
     </Setter> 
     <EventSetter 
     Event="Loaded" 
     Handler="PlaylistListBoxItem_Loaded" /> 
    </Style> 
    </ListBox.ItemContainerStyle> 
</ListBox> 
+0

謝謝帕夫洛! – Boris 2011-03-09 12:47:16