Asp.net 设置包含gridview的Listview的样式

Asp.net 设置包含gridview的Listview的样式,asp.net,wpf,xaml,listview,gridview,Asp.net,Wpf,Xaml,Listview,Gridview,我尝试设置listview模板的样式,该模板包含gridview。我试着使用ItemsPresenter来实现它,但gridview的标题消失了 我应该使用什么来保存标题 <Style TargetType="{x:Type ListView}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetT

我尝试设置listview模板的样式,该模板包含gridview。我试着使用ItemsPresenter来实现它,但gridview的标题消失了

我应该使用什么来保存标题

<Style TargetType="{x:Type ListView}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListView}">
                    <Border x:Name="Bd"
                            SnapsToDevicePixels="true">
                        <ScrollViewer Focusable="false">
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

编辑:我的列表视图

<ListView Name="findReplaceView" Margin="10" Grid.Row="1" Grid.Column="0" 
                  HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
                  SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                  ItemsSource="{Binding FindAndReplaceItems}">
  <ListView.View>
    <GridView AllowsColumnReorder="False">
      <GridViewColumn Header="Find" DisplayMemberBinding="{Binding Find}"
                                    Width="{Binding ElementName=helperField1, Path=ActualWidth}"/>
      <GridViewColumn Header="Replace" DisplayMemberBinding="{Binding Replace}"
                                    Width="{Binding ElementName=helperField2, Path=ActualWidth}"/>
    </GridView>
  </ListView.View>
</ListView>

这是因为您没有设置标题部分的样式。这是你应该做的:

<Style TargetType="{x:Type ListView}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Grid HorizontalAlignment="Left">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <!-- This is how your headers are presented -->
                        <ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ListView},Path=View.Columns}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Border BorderThickness="1" BorderBrush="LightGray" Background="MidnightBlue">
                                        <TextBlock Foreground="WhiteSmoke" 
                                                   TextAlignment="Center"
                                                   FontWeight="Bold" 
                                                   Text="{Binding Header}"
                                                   Width="{Binding ActualWidth}"/>
                                    </Border>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                        </ItemsControl>
                    <!-- This is how your items are presented -->
                    <Border x:Name="Bd" Grid.Row="1"
                    SnapsToDevicePixels="true">
                        <ScrollViewer Focusable="false">
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </ScrollViewer>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

在上面的代码中有两个
网格。行定义
带有
height=“Auto”
height=“*”
第一个是表示标题部分的区域(即顶行),第二个是包含
itemsresenter
的剩余区域,并确定项目的显示方式


编辑


由于这被认为是
ListView的通用样式,它的列已经在一些
ListView.View
中定义,因此我认为在标题部分使用
ItemsControl
将使您的样式非常灵活。我相应地修改了我的代码示例,其中标题部分是一个水平方向的
ItemsControl
,它的
ItemsSource
绑定到
ListView.View.Columns
。这样,样式中的列数将取决于
列表视图中的列数。另外,
ItemsControl.itemstemplate
是一个
TextBlock
,它的文本绑定到相应列的标题,因此样式中的列标题将与
列表视图中的列标题相同,这是因为您没有设置标题部分的样式。这是你应该做的:

<Style TargetType="{x:Type ListView}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Grid HorizontalAlignment="Left">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <!-- This is how your headers are presented -->
                        <ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ListView},Path=View.Columns}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Border BorderThickness="1" BorderBrush="LightGray" Background="MidnightBlue">
                                        <TextBlock Foreground="WhiteSmoke" 
                                                   TextAlignment="Center"
                                                   FontWeight="Bold" 
                                                   Text="{Binding Header}"
                                                   Width="{Binding ActualWidth}"/>
                                    </Border>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                        </ItemsControl>
                    <!-- This is how your items are presented -->
                    <Border x:Name="Bd" Grid.Row="1"
                    SnapsToDevicePixels="true">
                        <ScrollViewer Focusable="false">
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </ScrollViewer>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

在上面的代码中有两个
网格。行定义
带有
height=“Auto”
height=“*”
第一个是表示标题部分的区域(即顶行),第二个是包含
itemsresenter
的剩余区域,并确定项目的显示方式


编辑


由于这被认为是
ListView的通用样式,它的列已经在一些
ListView.View
中定义,因此我认为在标题部分使用
ItemsControl
将使您的样式非常灵活。我相应地修改了我的代码示例,其中标题部分是一个水平方向的
ItemsControl
,它的
ItemsSource
绑定到
ListView.View.Columns
。这样,样式中的列数将取决于
列表视图中的列数。另外,
ItemsControl.itemstemplate
是一个
TextBlock
,它的文本绑定到相应列的标题,因此,样式中的列标题将与
列表视图中的列标题相同

请为您的
列表视图
和内部的
网格视图
提供相关代码。添加了它,虽然我不明白为什么它很重要,但请为您的
列表视图
和内部的
网格视图
提供相关代码。添加了它,尽管我不明白为什么它很重要。尽管如此,这种方法将头名称锁定为始终相同的名称。有什么方法可以让它更灵活吗?你可以使用绑定从
列表视图中获取标题。像这样查看
就足够了,thnx对于help这种方法将标题名称锁定为始终相同的东西。有什么方法可以让它更灵活吗?您可以使用绑定从
ListView获取标题。查看
这样的
足够近,thnx获得帮助