带数据模板的 Wpf ItemsControl,某些项目的双边框问题

发布于 2024-10-10 10:32:01 字数 2023 浏览 0 评论 0原文

我有带有自定义数据模板的简单 ItemsControl,模板仅包含带边框的文本块。所有项目都应该一个接一个地垂直显示,但有些项目有额外的边框。 我怎样才能删除它?

我想实现类似于 enso launcher 的东西,它看起来像 alt text

我的实现如下

wpf textblock with border

这是我的 xaml 代码:

<Window x:Class="winmole.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" 
    x:Name="hostWindow"
    Height="Auto"
    MinHeight="100"
    MinWidth="100"
    Width="Auto"
    Padding="10"
    AllowsTransparency="True" WindowStyle="None" Background="Transparent"
    Top="0"
    Left="0"
    SizeToContent="WidthAndHeight"
    Topmost="True"
    Loaded="Window_Loaded"
    KeyUp="Window_KeyUp" 
    >
<Window.Resources>

    <!--Simple data template for Items-->
    <DataTemplate x:Key="itemsTemplate">
        <Border Background="Black" Opacity="0.9" HorizontalAlignment="Left" CornerRadius="0,2,2,0">
            <TextBlock  Text="{Binding Path=Title}" 
                 TextWrapping="Wrap" 
                 FontFamily="Georgia" FontSize="30" 
                 Height="Auto"
                 HorizontalAlignment="Left" 
                 VerticalAlignment="Stretch" 
                 TextAlignment="Left" Padding="5" Margin="0" Foreground="Yellow"/>

        </Border>

    </DataTemplate>
</Window.Resources>

<DockPanel>

    <ItemsControl DockPanel.Dock="Bottom" Name="itcPrompt"  
                  ItemsSource="{Binding ElementName=hostWindow, Path=DataItems}"
              ItemTemplate="{StaticResource itemsTemplate}"  >
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Vertical" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>

</DockPanel>

I have simple ItemsControl with custom datatemplate, template contains only textblock with border. All items should be displayed vertically one after another, but some items have extra border.
How can I remove it?

I want to achieve something similar to enso launcher, it looks like
alt text

My implementation looks like this

wpf textblock with border

here is my xaml code:

<Window x:Class="winmole.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" 
    x:Name="hostWindow"
    Height="Auto"
    MinHeight="100"
    MinWidth="100"
    Width="Auto"
    Padding="10"
    AllowsTransparency="True" WindowStyle="None" Background="Transparent"
    Top="0"
    Left="0"
    SizeToContent="WidthAndHeight"
    Topmost="True"
    Loaded="Window_Loaded"
    KeyUp="Window_KeyUp" 
    >
<Window.Resources>

    <!--Simple data template for Items-->
    <DataTemplate x:Key="itemsTemplate">
        <Border Background="Black" Opacity="0.9" HorizontalAlignment="Left" CornerRadius="0,2,2,0">
            <TextBlock  Text="{Binding Path=Title}" 
                 TextWrapping="Wrap" 
                 FontFamily="Georgia" FontSize="30" 
                 Height="Auto"
                 HorizontalAlignment="Left" 
                 VerticalAlignment="Stretch" 
                 TextAlignment="Left" Padding="5" Margin="0" Foreground="Yellow"/>

        </Border>

    </DataTemplate>
</Window.Resources>

<DockPanel>

    <ItemsControl DockPanel.Dock="Bottom" Name="itcPrompt"  
                  ItemsSource="{Binding ElementName=hostWindow, Path=DataItems}"
              ItemTemplate="{StaticResource itemsTemplate}"  >
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Vertical" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>

</DockPanel>

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

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

发布评论

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

评论(1

后知后觉 2024-10-17 10:32:01

如果我正确理解你的问题:尝试在边框上设置 SnapsToDevicePixels="True"

<Border SnapsToDevicePixels="True" Background="Black" Opacity="0.9" ...

If I understand your question correctly: Try to set SnapsToDevicePixels="True" on the Border

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