WPF中ListBoxItem控件模板设计的问题?

WPF中ListBoxItem控件模板设计的问题?,wpf,wpf-controls,controltemplate,Wpf,Wpf Controls,Controltemplate,这是我的XAML: <ListBox Name="PlaylistListBox" ScrollViewer.CanContentScroll="False" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Source={StaticResource ResourceKey=MyListBoxView}}" ItemTemplateSelector="{Binding Source={St

这是我的XAML:

<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>

两个问题:

  • 由于
    不透明度
    设置器,整个项目的透明度为50%。我只想要一个
    ListBoxItem
    ControlTemplate
    中定义的边框及其内容是透明的 以保持完全不透明度
  • 列表框
    不可用时,如何使Setter/Trigger使相同的边框变为红色 聚焦?它应该类似于
    InFocus=“False”
    IsSelected=“True”
    谢谢你的澄清

  • 您应该在内容下方放置另一个边框,使其半透明,使主要内容完全可见。您可以通过使用
    网格
    并首先在其中放置一个“背景”边框,然后再放置内容来完成此操作。这样,您将只在背景边框上设置不透明度,而不在内容上设置不透明度

  • 您可以使用
    multi-trigger

  • 下面是修改后的样式,它显示了我的意思:

    <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>