【问题标题】:Styling a Listview with a gridview in it使用其中的 gridview 为 Listview 设置样式
【发布时间】:2015-07-12 10:26:12
【问题描述】:

我尝试设置包含网格视图的列表视图模板的样式。我尝试使用 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>

【问题讨论】:

  • 请提供您的listview和里面的gridview的相关代码。
  • 添加了它,虽然我不明白它为什么重要

标签: asp.net wpf xaml listview gridview


【解决方案1】:

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

<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>

在上面的代码中有 2 个Grid.RowDefinitionsheight="Auto"height="*" 第一个是代表标题部分的区域(即顶行),第二个是包含您的@的剩余区域987654325@ 并确定您的项目的呈现方式。


编辑

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

【讨论】:

  • 这种方式将标题名称锁定为始终相同。有什么办法可以让它变得灵活吗?
  • 您可以使用绑定从ListView.View 获取标题,例如&lt;TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=ListView},Path=View.Columns[0].Header}",但是您仍然必须在您的样式中指定列的计数。
  • 足够接近,谢谢帮助
猜你喜欢
  • 2015-04-17
  • 2012-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多