在WPF拉伸控制,以填补一个ListView列 [英] In WPF Stretch a control to fill a ListView Column

查看:183
本文介绍了在WPF拉伸控制,以填补一个ListView列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何做一个控制填补在ListView全细胞(用一个GridView)?我打得周围的各种属性,但控制始终是它的最小尺寸。

How do I make a control fill the whole cell in a ListView (with a GridView)? I've played around with various properties, but the control is always it's minimum size.

下面是我的XAML。

<Window x:Class="ListViewUserControlTest.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:ListViewUserControlTest"
  Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <DataTemplate x:Key="UserControlCell">
            <Rectangle Fill="Red" HorizontalAlignment="Stretch" MinWidth="10" MinHeight="10" />              
        </DataTemplate>
    </Window.Resources>
    <Grid>        
        <ListView Name="MyListView">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="30" Header="Col1" DisplayMemberBinding="{Binding Name}"  />
                    <GridViewColumn Width="200" Header="Col2" CellTemplate="{StaticResource UserControlCell}"  />
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>

修改对不起,我改变了这个问题,因为我已经消除了用户控件作为问题的根源。它发生任何控制权。

Edit Sorry I changed the question because I had eliminated the user control as the source of the problem. It happens with any control.

推荐答案

缩小我的问题后,我正要谷歌和找到的回答这里

After narrowing down my question I was about to Google and find an answer here.

基本上由于某种原因ListViewItems被设定为对准左。将它们设置为拉伸修复了这个问题。这是通过一个样式做像这样:

Basically for some reason the ListViewItems are set to align to the Left. Setting them to Stretch fixes this problem. This is done through a style like so:

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

这不幸影响到每一个列,但你可以设置其他列的内容,左,右,中心如链接解释。

This unfortunately affects every column, but then you can set the contents of other columns to left, right, center as explained in the link.

这篇关于在WPF拉伸控制,以填补一个ListView列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆