【问题标题】:How to resize width of an image in a listview in WPF?如何在 WPF 的列表视图中调整图像的宽度?
【发布时间】:2019-04-30 12:59:24
【问题描述】:

我有一个 ListView,其中包含 Image 作为项目。每当我调整包含它的 Grid 的大小时,我希望每个图像的宽度都遵循 ListView 的宽度。

<!--Playlists-->
<Grid Column="0">
    <ListView Grid.Row="1" Grid.Column="1"
              HorizontalAlignment="Stretch"
              ... >

        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            </Style>
        </ListView.ItemContainerStyle>

        <!--Images-->
        <ListView.ItemTemplate>
            <DataTemplate DataType="{x:Type models:PlaylistModel}">
                <Grid HorizontalAlignment="Stretch">
                    <Image Source="{Binding ImagePath}"
                           Stretch="Uniform">
                    </Image>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>

    </ListView>
</Grid>

<GridSplitter Grid.Column="0" />

来源:http://www.teixeira-soft.com/bluescreen/2016/03/21/c-how-to-make-a-panel-within-a-datatemplate-fill-the-entire-width-of-a-listview-or-itenscontrol-derivative/

This is what happens

如图所示,图像的宽度根本没有调整大小。即使 ListView 的大小发生变化,图像大小也保持不变。但是,如果我在DataTemplate 内设置Grid 的宽度,那么图像实际上会遵循该宽度。

【问题讨论】:

    标签: c# wpf xaml listview


    【解决方案1】:

    看来使用ListView 是个错误。将所有内容更改为 ListBox 已为我修复。

    如果有人遇到同样的问题,请留在这里。

    <ListBox HorizontalAlignment="Stretch">
    
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            </Style>
        </ListBox.ItemContainerStyle>
    
        <ListBox.ItemTemplate>
            <DataTemplate DataType="{x:Type models:PlaylistModel}">
                <Image Source="{Binding ImagePath}"
                           Stretch="Uniform" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-17
      相关资源
      最近更新 更多