5

我正在使用带有 Itemtemplate 和 Headertemplate 的 Listview。两个模板都包含 6 列。如果我为模板设置一个固定的列宽,一切都很好——如图一所示。
但我想将项目的宽度设置为“自动” - 但后来我得到图 2 ......

如何处理?是否可以使用 c# 设置标题列宽度?- 或任何其他解决方案?

图1:

http://i.stack.imgur.com/8Ew3g.png

图 2:

http://i.stack.imgur.com/mPX4U.png

代码列表视图:

<ListView x:Name="DayanalyseListView" 
                      HorizontalAlignment="Center" 
                      VerticalAlignment="Top" 
                      ItemTemplate="{StaticResource DataTemplate}" 
                      HeaderTemplate="{StaticResource HeaderTemplate}">
            </ListView>

标题模板:

<DataTemplate  x:Key="HeaderTemplate" >
        <Grid Height="36" Background="DarkGray" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="95"/>
                <ColumnDefinition Width="85"/>
                <ColumnDefinition Width="85"/>
                <ColumnDefinition Width="85"/>
                <ColumnDefinition Width="85"/>
                <ColumnDefinition Width="*" MinWidth="900"/>
            </Grid.ColumnDefinitions>
            <TextBlock x:Uid="DayProject" TextWrapping="Wrap" Text="Project" Grid.Column="0" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="7,2,0,0" Style="{StaticResource BodyTextStyle}" />
            <TextBlock x:Uid="DayTask" TextWrapping="Wrap" Text="Task" Grid.Column="1" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="7,2,0,0" Style="{StaticResource BodyTextStyle}" />
            <TextBlock x:Uid="DayFrom" TextWrapping="Wrap" Text="From" Grid.Column="2" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="7,2,0,0" Style="{StaticResource BodyTextStyle}" />
            <TextBlock x:Uid="DayTill" TextWrapping="Wrap" Text="Till" Grid.Column="3" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="7,2,0,0" Style="{StaticResource BodyTextStyle}" />
            <TextBlock x:Uid="DaySum" TextWrapping="Wrap" Text="Sum"  Grid.Column="4" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="7,2,0,0" Style="{StaticResource BodyTextStyle}" />
            <TextBlock x:Uid="DayNote" TextWrapping="Wrap" Text="Note"  Grid.Column="5" VerticalAlignment="Top" HorizontalAlignment="Left"  Margin="7,2,0,0" Style="{StaticResource BodyTextStyle}" />
        </Grid>
    </DataTemplate>

项目模板:

<DataTemplate x:Key="DataTemplate">
        <Grid d:DesignHeight="50" Margin="0,5,0,0" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="95"/>
                <ColumnDefinition Width="85"/>
                <ColumnDefinition Width="85"/>
                <ColumnDefinition Width="85"/>
                <ColumnDefinition Width="85"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock HorizontalAlignment="Left" TextWrapping="NoWrap" Text="{Binding ProjectName}" VerticalAlignment="Center" Margin="5,0,0,0" Style="{StaticResource ItemTextStyle_sf}" Grid.Column="0" ToolTipService.ToolTip="{Binding ProjectName}"/>
            <TextBlock HorizontalAlignment="Left" TextWrapping="NoWrap" Text="{Binding TaskName}" VerticalAlignment="Center" Margin="5,0,0,0" Style="{StaticResource ItemTextStyle_sf}" Grid.Column="1" ToolTipService.ToolTip="{Binding TaskName}"/>
            <TextBlock HorizontalAlignment="Left" TextWrapping="NoWrap" Text="{Binding StartTimeString}" VerticalAlignment="Center" Margin="5,0,0,0" Style="{StaticResource ItemTextStyle_sf}"  Grid.Column="2"/>
            <TextBlock HorizontalAlignment="Left" TextWrapping="NoWrap" Text="{Binding StopTimeString}" VerticalAlignment="Center" Margin="5,0,0,0" Style="{StaticResource ItemTextStyle_sf}"  Grid.Column="3"/>
            <TextBlock HorizontalAlignment="Left" TextWrapping="NoWrap" Text="{Binding Sum}" VerticalAlignment="Center" Margin="5,0,0,0" Style="{StaticResource ItemTextStyle_sf}"  Grid.Column="4"/>
            <TextBlock HorizontalAlignment="Left" TextWrapping="NoWrap" Text="{Binding Note}" VerticalAlignment="Center" Margin="5,0,0,0" Style="{StaticResource ItemTextStyle_sf}"  ToolTipService.ToolTip="{Binding Note}" Grid.Column="5"/>
        </Grid>
     </DataTemplate>
4

6 回答 6

6

以下在我的情况下非常有效:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid x:Name="ListViewHeaders" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition  />
            <ColumnDefinition  />
            <ColumnDefinition  />
            <ColumnDefinition  />
            <ColumnDefinition  />
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="0" Text="Title Col 1" />
        <TextBlock Grid.Column="1" Text="Title Col 2" />
        <TextBlock Grid.Column="2" Text="Title Col 3" />
        <TextBlock Grid.Column="3" Text="Title Col 4" />
        <TextBlock Grid.Column="4" Text="Title Col 5" />
    </Grid>
    <ListView x:Name="myTable" Grid.Row="1" ItemsSource="{Binding SomeCollection}" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid Width="{Binding ElementName=myTable, Path=ActualWidth}" >
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition  />
                        <ColumnDefinition  />
                        <ColumnDefinition  />
                        <ColumnDefinition  />
                        <ColumnDefinition  />
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" Text="{Binding ItemProperty1}" />
                    <TextBlock Grid.Column="1" Text="{Binding ItemProperty2}" />
                    <TextBlock Grid.Column="2" Text="{Binding ItemProperty3}" />
                    <TextBlock Grid.Column="3" Text="{Binding ItemProperty4}" />
                    <TextBlock Grid.Column="4" Text="{Binding ItemProperty5}" />
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>
于 2013-03-03T23:08:00.553 回答
5

ListView 不像 DataGrid 那样运行。ListView 只理解行,不理解列。您可以像使用 Grid 一样模拟列,但要了解的重要一点是项目和标题是两个完全独立的 UI 元素。

这意味着项目中列的大小很难改变标题中列的大小。即使您要命名它们,它们也是模板的一部分,因此名称不会有任何用处(请记住,将创建多个项目)。

我建议您保持固定宽度、使用 * 宽度或使用实际的 DataGrid 控件。

于 2013-01-22T19:15:00.463 回答
2

以下对我有用。关键是将 Horizo​​ntalContentAlignment 属性设置为 Stretch。

<ListView Grid.Row="6" Name="lvMembersSearchResults" Background="LightGray" ItemClick="lvMembersSearchResults_ItemClick" IsItemClickEnabled="True" Margin="5,5,5,5">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment"  Value="Stretch"></Setter>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.Header>
                <Style TargetType="ListViewHeaderItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
                </Style>
            </ListView.Header>
            <ListView.HeaderTemplate>
                <DataTemplate>
                    <Grid Height="30" Background="DarkGray">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="60"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="70"/>
                            <ColumnDefinition Width="50"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock x:Uid="tbRank" TextWrapping="Wrap" Text="Rank" Grid.Column="0" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="7,2,0,0" FontWeight="ExtraBold" FontSize="18" />
                        <TextBlock x:Uid="tbName" TextWrapping="Wrap" Text="Name" Grid.Column="1" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="7,2,0,0"  FontWeight="ExtraBold" FontSize="18" />
                        <TextBlock x:Uid="tbTaxId" TextWrapping="Wrap" Text="Tax ID" Grid.Column="2" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="7,2,0,0" FontWeight="ExtraBold" FontSize="18" />
                        <TextBlock x:Uid="tbCommand" TextWrapping="Wrap" Text="Cmd" Grid.Column="3" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="7,2,0,0" FontWeight="ExtraBold" FontSize="18" />
                    </Grid>
                </DataTemplate>
            </ListView.HeaderTemplate>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid Height="30" Background="DarkBlue">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="60"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="70"/>
                            <ColumnDefinition Width="50"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock TextWrapping="NoWrap"  Text="{Binding Rank}" Grid.Column="0" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="7,2,0,0" FontWeight="Bold"  FontSize="18"  />
                        <TextBlock TextWrapping="NoWrap" Text="{Binding Name}" Grid.Column="1" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="7,2,0,0" FontWeight="Bold"  FontSize="18"/>
                        <TextBlock TextWrapping="NoWrap" Text="{Binding TaxId}" Grid.Column="2" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="7,2,0,0" FontWeight="Bold"  FontSize="18"/>
                        <TextBlock TextWrapping="NoWrap" Text="{Binding Command}" Grid.Column="3" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="7,2,0,0" FontWeight="Bold"  FontSize="18"/>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
于 2016-03-02T13:58:29.510 回答
1

添加 UserControl 对我有用。

<ListView x:Name="listView" ItemsSource="{Binding SomeCollection}" >
   <ListView.ItemTemplate>
       <DataTemplate>
           <UserControl Width="{Binding ElementName=listView, Path=ActualWidth}">
               <Grid> 
                 ....
                </Grid>
            </UserControl>
       </DataTemplate>
   </ListView.ItemTemplate>
</ListView>`
于 2013-11-04T12:19:57.533 回答
1

要将项目拉伸到完整的 ListView 宽度,请尝试以下操作:

<ListView>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="80" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{Binding Number}" />
                <TextBlock Grid.Column="1" Text="{Binding Text}" />
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
于 2014-09-17T22:27:59.703 回答
0

您可以尝试使用计算每列宽度的属性创建自己的 UserControl,并在模板中绑定到此属性。可能是这个帮助。

于 2013-09-20T07:02:42.770 回答