SelectedItem 的 Horizo​​ntalContentAlignment 未完美拉伸

发布于 2024-09-25 10:54:40 字数 1682 浏览 0 评论 0原文

我在 WPF 中制作了一个自定义样式列表:

        <DataTemplate x:Key="SelectedTemplate">
            <StackPanel Background="#FF4E4E4E" HorizontalAlignment="Stretch">
                <TextBlock Text="{Binding Path=Title}" Foreground="#FFD80000" />
            </StackPanel>
        </DataTemplate>

        <Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
            <Setter Property="ContentTemplate" 
                    Value="{StaticResource ItemTemplate}" />
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="ContentTemplate"
                            Value="{StaticResource SelectedTemplate}" />
                </Trigger>
            </Style.Triggers>
        </Style>

...

    <ListBox 
        Name="lbSongs"
        DockPanel.Dock="Left" 
        ItemsSource="{Binding Path=SongDirectory}"
        SelectedItem="{Binding Path=Song, Mode=TwoWay}"
        Visibility="{Binding Path=ListVisibility}"
        ItemContainerStyle="{StaticResource ContainerStyle}"
        HorizontalContentAlignment="Stretch"
        Width="180px" Background="#FF333333" />

我尝试为所选项目制作自定义样式。为了使选择栏拉伸到列表框的宽度,我将 ItemContainerStyle 的 Horizo​​ntalContentAlignment 属性设置为“Stretch”。问题是它没有完全拉伸,左侧仍然有一个小条,并且原始(蓝色)选择条仍然可见。请参阅屏幕截图:

alt text

如何使其拉伸到完整尺寸?或者如何设置原始选择栏的样式?

提前致谢。

I made a custom styled list in WPF:

        <DataTemplate x:Key="SelectedTemplate">
            <StackPanel Background="#FF4E4E4E" HorizontalAlignment="Stretch">
                <TextBlock Text="{Binding Path=Title}" Foreground="#FFD80000" />
            </StackPanel>
        </DataTemplate>

        <Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
            <Setter Property="ContentTemplate" 
                    Value="{StaticResource ItemTemplate}" />
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="ContentTemplate"
                            Value="{StaticResource SelectedTemplate}" />
                </Trigger>
            </Style.Triggers>
        </Style>

...

    <ListBox 
        Name="lbSongs"
        DockPanel.Dock="Left" 
        ItemsSource="{Binding Path=SongDirectory}"
        SelectedItem="{Binding Path=Song, Mode=TwoWay}"
        Visibility="{Binding Path=ListVisibility}"
        ItemContainerStyle="{StaticResource ContainerStyle}"
        HorizontalContentAlignment="Stretch"
        Width="180px" Background="#FF333333" />

I tried to make a custom style for the selected item. To make the selection bar stretch to the width of the ListBox, I set the ItemContainerStyle's HorizontalContentAlignment property to "Stretch". The problem is that it does not stretch fully, a tiny bar on the left still remains and the original (blue) selection bar is still visible there. See the screenshot:

alt text

How can I make it to stretch to full size? Or how can I style the original selection bar?

Thanks in advance.

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

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

发布评论

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

评论(1

爱情眠于流年 2024-10-02 10:54:40

只需将 ListBoxItem 的 Padding 设置为零即可。那个蓝色条应该消失。

 <Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
      <Setter Property="Padding" Value="0"/>
      ...
 </Style>

另一种选择是重写 ListBoxItem 的 ControlTemplate,以便您可以更好地控制它的外观。但是,是的,这对于您的情况可能没有必要。

Just set the Padding for the ListBoxItem to zero. That blue bar should disappear.

 <Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
      <Setter Property="Padding" Value="0"/>
      ...
 </Style>

Another option is just to re-write the ControlTemplate for the ListBoxItem so that you'll have better control on how it looks like. But yeah, this may not be necessary in your case.

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