WPF 中的 ListBoxItem ControlTemplate 设计存在问题?

发布于 2024-10-21 03:44:57 字数 1961 浏览 10 评论 0原文

这是我的 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>

两个问题:

  1. 由于 Opacity setter,整个项目透明 50%。我想要的只是 ListBoxItem ControlTemplate 中定义的边框为透明,其内容 以保持完全不透明度。
  2. ListBox 不存在时,如何制作 Setter/Trigger 以使相同的边框变为红色 焦点集中?它应该类似于 InFocus="False"IsSelected="True"

感谢您的澄清。

Here's my 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>

Two issues:

  1. Because of the Opacity setter, whole item is transparent by 50%. I want just the
    border defined in the ListBoxItem ControlTemplate to be transparent and its content
    to preserve full opacity.
  2. How do I make a Setter/Trigger to make that same border red when the ListBox is not
    in focus? It should be something like InFocus="False" and IsSelected="True".

Thanks for clarifying.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹤舞 2024-10-28 03:44:57
  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>
  1. You should place another border underneath the content and make it half-transparent remaining the main content fully visible. You can accomplish this by using Grid and placing a "background" border in it first and then the content. This way you will set the opacity only on the background border, but not on the content;

  2. You can use a MultiTrigger for that.

Here is the modified style that shows what I mean:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文