【问题标题】:Populate ComboBox in GridView header with column data使用列数据填充 GridView 标题中的 ComboBox
【发布时间】:2009-05-18 10:19:31
【问题描述】:

我想在我的GridView(当前位于ListView 内)的标题单元格中添加一个ComboBox,并填充该列中的数据。使用起来有点像 Excel 中的“自动筛选”功能。

我遇到的问题是我无法从标题单元格内获取列的数据。目前,我有这样的事情:

<Grid>
  <Grid.Resources>
    <DataTemplate x:Key="headerTemplate">
     <StackPanel>
        <ComboBox></ComboBox>
      <TextBlock FontSize="16" Text="{Binding}" Grid.Row="1" HorizontalAlignment="Center"/>
      </StackPanel>
    </DataTemplate>
  </Grid.Resources>
  <ListView Width="400" Height="200">
    <ListView.View>
      <GridView ColumnHeaderTemplate="{StaticResource headerTemplate}">
          <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
          <GridViewColumn Header="Namespace" DisplayMemberBinding="{Binding Namespace}"/>
      </GridView>
    </ListView.View>
    <x:Type TypeName="Visual"/>
    <x:Type TypeName="UIElement"/>
    <x:Type TypeName="FrameworkElement"/>
  </ListView>
</Grid>

但我不确定如何获取数据以填充标题中的组合框。这样做的最佳方法是什么?

【问题讨论】:

    标签: wpf gridview combobox


    【解决方案1】:

    一种快速而肮脏的方式是这样的:

    <Grid.Resources>
        <DataTemplate x:Key="headerTemplate">
            <StackPanel>
                <ComboBox ItemsSource="{Binding Path=Items, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}}" DisplayMemberPath="{Binding}"/>
                <TextBlock FontSize="16" Text="{Binding}" Grid.Row="1" HorizontalAlignment="Center"/>
            </StackPanel>
        </DataTemplate>
    </Grid.Resources>
    

    如果你真的需要一个类似过滤器的列表,你必须弄清楚如何只获取不同的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-17
      • 2018-08-03
      • 2014-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-20
      相关资源
      最近更新 更多