WPF - 在列表框项目中使用网格

时间:2014-06-25 00:30:49

标签: c# wpf listview grid listviewitem

首先,我是WPF的初学者,所以请用简单的词语解释。

我想制作包含很多元素的自定义列表视图项。我决定使用网格来很好地对齐它。不幸的是,当我试图查看是否所有内容都正在调整时,背景(listview项目)调整大小而网格不是。我认为这是因为网格没有正确附加到listview项目。所以问题是我如何将网格附加到listview项目,以便我可以正确地缩放内容?

这是代码(由于我试图解决问题,它有点乱):

<Window
    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" x:Class="SurfManager.MainWindow"
    Title="MainWindow" Height="503" Width="933">
<Window.Triggers>
    <EventTrigger RoutedEvent="FrameworkElement.Loaded"/>
</Window.Triggers>
<Grid>
    <TabControl>
        <TabItem Header="Obecne Wypożyczenia" Background="White" Foreground="Black" BorderBrush="#FF8C8E94" OpacityMask="White">
            <Grid Background="#FFE5E5E5">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <ListView BorderThickness="0" Height="440">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn/>
                        </GridView>
                    </ListView.View>
                    <ListViewItem>
                        <ListViewItem.Background>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="#FF2AD618" Offset="0"/>
                                <GradientStop Color="#FF6CFF5D" Offset="0.582"/>
                            </LinearGradientBrush>
                        </ListViewItem.Background>
                        <Grid Height="82.96" Width="859.733">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="41*"/>
                                <RowDefinition Height="42*"/>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="0*"/>
                                <ColumnDefinition Width="180*"/>
                                <ColumnDefinition Width="283*"/>
                                <ColumnDefinition Width="387*"/>
                                <ColumnDefinition Width="140*"/>
                            </Grid.ColumnDefinitions>
                            <Label Content="Imię:"  Margin="8.001,10,0,38" Grid.RowSpan="2" Grid.Column="1" Grid.ColumnSpan="2" />
                            <Label Content="Nazwisko:"  Margin="8.001,5.451,0,3.529" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"/>
                            <Label Content="Początek:" HorizontalAlignment="Left" Margin="0.978,10,0,0" VerticalAlignment="Top" Height="27.96" Width="59.733" Grid.Column="2"/>
                            <Label Content="Koniec:" HorizontalAlignment="Left" Margin="0.978,1.92,0,0" VerticalAlignment="Top" Height="33" Grid.Row="1" Width="48.107" Grid.Column="2"/>
                            <Label Content="Pozostało" HorizontalAlignment="Left" Margin="37,10,0,0" VerticalAlignment="Top" Height="25.96" Width="61.79" Grid.Column="3"/>
                            <Label Content="Cena:" HorizontalAlignment="Left" Margin="37,8.96,0,0" VerticalAlignment="Top" Grid.Row="1" Height="25.96" Width="39.204" Grid.Column="3"/>
                            <Label Content="Deska" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,5.02,0,0" Height="25.96" Width="41.853" Grid.Column="4"/>
                            <Label Content="Żagiel" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="9.746,6.02,0,0" Height="25.96" Width="42.107" Grid.Column="4" Grid.Row="1"/>
                        </Grid>
                    </ListViewItem>
                </ListView>
            </Grid>
        </TabItem>
        <TabItem Header="Baza Klientow">
            <Grid Background="#FFE5E5E5"/>
        </TabItem>
        <TabItem Header="Administracja sprzętem" HorizontalAlignment="Left" Height="19.96" VerticalAlignment="Top" Width="144.32" Margin="-2,-2,-7,0">
            <Grid Background="#FFE5E5E5"/>
        </TabItem>
    </TabControl>
</Grid>
</Window>

2 个答案:

答案 0 :(得分:1)

这就是你需要的东西

在列表视图中添加此样式资源,并从网格中删除宽度。

    <ListView BorderThickness="0"
                Height="440">
        <ListView.Resources>
            <Style TargetType="ListViewItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListViewItem">
                            <Border Background="{TemplateBinding Background}">
                                <ContentPresenter />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListView.Resources>

通过从列表视图项修改默认teplate将使内容重新调整大小

也删除以下内容

<ListView.View>
    <GridView>
        <GridViewColumn/>
    </GridView>
</ListView.View>

答案 1 :(得分:1)

ListView通常使用如下:

使用绑定:

<ListView BorderThickness="0" >
      <ListView.View>
              <GridView>
                 <GridViewColumn Header="Name" Width="200" DisplayMemberBinding="{Binding Name}"/>
                 <GridViewColumn Header="Address" Width="200" DisplayMemberBinding="{Binding Address}"/>
               </GridView>
</ListView.View>

或者像这样:

 <ListView BorderThickness="0" >
     <ListViewItem>
          <Grid >
             ....
          </Grid>
      </ListViewItem>
  </ListView>

您确定需要同时设置ListView.ViewListViewItem吗?它看起来像: enter image description here

顺便说一句,你不应该设置网格的宽度和高度,最重要的是你的网格内容看起来很混乱。你必须在正确的Grid.Col或Grid.Row中设置这些标签,为什么你设置ColumnSpan / RowSpan?如果你确定它是正确的?