Wpf ListView右边框切断

时间:2017-05-14 17:44:43

标签: c# wpf listview listviewitem

我尝试使用3条文本行创建WPF列表视图但右侧边框未正确设置(切除约1-3像素 right border。左手边很好。

<Window x:Class="ListViewBorder.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ListViewBorder"
    mc:Ignorable="d"
    x:Name="MainWindowUc"
    Title="MainWindow" Height="800" Width="1024">
<Grid>
    <ListView
        ItemsSource="{Binding ElementName=MainWindowUc, Path=Items}"
        BorderBrush="DarkCyan"
        BorderThickness="1" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
        >
        <ListView.ItemTemplate>
            <DataTemplate>
                <Border Background="DarkGray"
                        BorderBrush="Blue" 
                        BorderThickness="2"
                        CornerRadius="6" 
                        ScrollViewer.VerticalScrollBarVisibility="Disabled"
                        Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}">
                    <WrapPanel Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=Border}}"
                               >

                        <TextBlock Padding="1" Text="{Binding Column1}" Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=WrapPanel}}" TextWrapping="NoWrap"></TextBlock>
                        <TextBlock Padding="1" Text="{Binding Column2}" Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=WrapPanel}}" TextWrapping="Wrap"></TextBlock>
                        <TextBlock Padding="1" Text="{Binding Column3}" Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=WrapPanel}}" TextWrapping="Wrap" TextAlignment="Right"></TextBlock>

                    </WrapPanel>

                </Border>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

如何&#34;修复&#34;此?

2 个答案:

答案 0 :(得分:0)

您不必在任何地方显示宽度,只需删除宽度绑定并尝试:

<ListView BorderBrush="DarkCyan"
        BorderThickness="1"
        ItemsSource="{Binding ElementName=MainWindowUc, Path=Items}"
        HorizontalContentAlignment="Stretch"
        ScrollViewer.HorizontalScrollBarVisibility="Disabled"
        ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ListView.ItemTemplate>
    <DataTemplate>
        <Border Background="DarkGray"
                BorderBrush="Blue"
                BorderThickness="2"
                CornerRadius="6"
                Padding="6"
                ScrollViewer.VerticalScrollBarVisibility="Disabled">
            <StackPanel>
                <TextBlock Padding="1"
                            Text="{Binding Column1}"
                            TextWrapping="NoWrap" />
                <TextBlock Padding="1"
                            Text="{Binding Column2}"
                            TextWrapping="Wrap" />
                <TextBlock Padding="1"
                            Text="{Binding Column3}"
                            TextAlignment="Right"
                            TextWrapping="Wrap" />

            </StackPanel>
        </Border>
    </DataTemplate>
</ListView.ItemTemplate>

密钥如下:HorizontalContentAlignment="Stretch"

这看起来像这样:

Example Image

答案 1 :(得分:0)

如果其他人遇到此问题,并且设置HorizontalContentAlignment="Stretch"无济于事:

我唯一能解决的问题就是彻底将ListViewItem的width属性设置为比ViewportWidth的{​​{1}}小一些。内。

ScrollViewer

然后在<Window.Resources> <Style x:Key="MyStyle" TargetType="ListViewItem"> <Setter Property="Width" Value="{Binding ViewportWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ScrollViewer}, Converter={StaticResource ABitLessConverter}}" /> <Setter Property="BorderBrush" Value="LightGray" /> <Setter Property="BorderThickness" Value="1" /> </Style> </Window.Resources> 中输入

ListView

这是<ListView ItemContainerStyle="{StaticResource MyStyle}"> <!-- Contents --> </ListView> 的代码,它减去了一点:

ABitLessConverter

当然,要使class ABitLessConverter : IValueConverter { private const int REDUCTION_VALUE = 2; public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { double intValue = (double)value; return intValue > REDUCTION_VALUE ? intValue - REDUCTION_VALUE : value; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotSupportedException(); } } 正常工作,您需要使其成为某个密钥为{StaticResource ABitLessConverter}的资源。