ListView中的图像未完全居中

时间:2018-09-02 12:55:14

标签: wpf listview

我正在尝试在ListView中显示图像列表。我希望这些图像显示在“项目”的中心。

我试图通过将相应的VerticalAlignment和Horizo​​ntalAlignment设置为居中来实现此目的。这不会给我我想要的结果。选择一个项目后,您可以在左侧清楚地看到一个较大的“蓝色”区域。

我想念什么吗?

GUI示例: Image not in center example

我的代码:

<Window x:Class="MyClass.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"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
  <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="3*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="3*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <ListView Grid.Column="0" Grid.Row="0" Margin="5" Name="lvImages" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel  Orientation="Horizontal"  ItemHeight="100" ItemWidth="100"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ListView.ItemTemplate>
            <DataTemplate >
                <Image Source="{Binding Bitmap}" Margin="2"  VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="UniformToFill" />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    <Button  Grid.Column="1" Grid.Row="0" Margin="5">Picture details</Button>
    <Button Grid.Column="0" Grid.Row="1" Margin="5">Document options</Button>
    <Button Grid.Column="1" Grid.Row="1" Margin="5">Go go go area</Button>
  </Grid>
</window>

PS:对不起,您偷了Cookie! OMNOMNOMNOM !!!

1 个答案:

答案 0 :(得分:0)

有两种方法 1.设置GridViewColumn的ActualWidth  而且必须添加边框

        <ListView.View>

            <GridView >

                <GridViewColumn Header="Name"   Width="200" x:Name="column">

                    <GridViewColumn.CellTemplate>

                        <DataTemplate>

                            <TextBlock Text="{Binding Name}" TextAlignment="Center" MinWidth="{Binding Path=ActualWidth,ElementName=column}"/>

                        </DataTemplate>

                    </GridViewColumn.CellTemplate>

                </GridViewColumn>

                <GridViewColumn Header="GetRes"  Width="120" x:Name="sss">

                    <GridViewColumn.CellTemplate>

                        <DataTemplate>

                            <Border  MinWidth="{Binding Path=ActualWidth,ElementName=sss}">

                                <CheckBox IsChecked="{Binding IsReceive}" HorizontalAlignment="Center"/>

                            </Border>

                        </DataTemplate>

                    </GridViewColumn.CellTemplate>

                </GridViewColumn>

            </GridView>

        </ListView.View>

    </ListView>

2.set样式

    <Style x:Key="ListViewItemStyle" TargetType="{x:Type ListViewItem}">

        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>

    </Style>