ListView中的DataGrid作为项目模板

时间:2016-07-22 08:58:58

标签: wpf listview datagrid

我在使用DataGrids显示ListView时遇到了一些问题(作为项目模板)。问题是DataGrid中的列宽度大小错误而不是拉伸以填充整个控件宽度。

代码:

<Window x:Class="WpfTest1.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"
        xmlns:local="clr-namespace:WpfTest1"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>

        <x:Array x:Key="ListViewItems"
                 Type="{x:Type system:String}">
            <system:String>1</system:String>
            <system:String>2</system:String>
        </x:Array>

        <x:Array x:Key="DataGridItems"
                 Type="{x:Type system:String}">
            <system:String>1</system:String>
            <system:String>2</system:String>
            <system:String>3</system:String>
        </x:Array>

        <DataTemplate x:Key="TemplateWidthDataGrid">
            <DataGrid ItemsSource="{StaticResource DataGridItems}">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Column1"
                                        Width="*" />
                    <DataGridTextColumn Header="Column2"
                                        Width="*" />
                </DataGrid.Columns>
            </DataGrid>
        </DataTemplate>

    </Window.Resources>

    <Grid>
        <ListView ItemTemplate="{StaticResource TemplateWidthDataGrid}"
                  ItemsSource="{StaticResource ListViewItems}" />
    </Grid>
</Window>

结果如下所示: MainWindow screenshot

如何在此场景中获取填充控件宽度的列?

已更新

根据Ciccio的回答,我在ListViewItem中设置了HorizontalContentAlignment属性。

    <ListView ItemTemplate="{StaticResource TemplateWithDataGrid}"
              ItemsSource="{StaticResource ListViewItems}">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment"
                        Value="Stretch" />
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>

结果不满意: Another screenshot

(我还必须为AutoGenerateColumns="False"设置DataGrid。)

将此与DataGrid之外的同一ListView进行比较,如下所示:

<Grid>
    <DataGrid ItemsSource="{StaticResource DataGridItems}"
              AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Column1"
                                Width="*" />
            <DataGridTextColumn Header="Column2"
                                Width="*" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>

Third screenshot

1 个答案:

答案 0 :(得分:1)

问题是ItemPanel没有扩展其内容。您应该修复它,为 ListBoxItem 设置属性bluetoothGatt.setCharacteristicNotification(characteristicToSubscribeTo, true);

HorizontalContentAlignment

修改

根据我发现的Here,您可能会使用解决方法解决此问题:(将数据网格的宽度绑定到 ListViewItemPresenter 的ActualWidth 并调整保证金)

<Grid>
    <ListView ItemTemplate="{StaticResource TemplateWidthDataGrid}"
              ItemsSource="{StaticResource ListViewItems}" >
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>
</Grid>