WPF 中的 ListBoxItem ControlTemplate 设计存在问题?
这是我的 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>
两个问题:
- 由于
Opacity
setter,整个项目透明 50%。我想要的只是ListBoxItem
ControlTemplate
中定义的边框为透明,其内容 以保持完全不透明度。 - 当
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:
- Because of the
Opacity
setter, whole item is transparent by 50%. I want just the
border defined in theListBoxItem
ControlTemplate
to be transparent and its content
to preserve full opacity. - How do I make a Setter/Trigger to make that same border red when the
ListBox
is not
in focus? It should be something likeInFocus="False"
andIsSelected="True"
.
Thanks for clarifying.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在内容下方放置另一个边框,并使其半透明,保持主要内容完全可见。您可以通过使用 Grid 并首先在其中放置“背景”边框,然后再放置内容来实现此目的。这样,您将仅在背景边框上设置不透明度,而不是在内容上设置不透明度;
您可以为此使用
MultiTrigger
。这是修改后的样式,显示了我的意思:
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;You can use a
MultiTrigger
for that.Here is the modified style that shows what I mean: