所有列的 WPF GridView 共享单元格模板
Posted
技术标签:
【中文标题】所有列的 WPF GridView 共享单元格模板【英文标题】:WPF GridView shared cell template for all columns 【发布时间】:2013-04-04 01:07:42 【问题描述】:我正在尝试创建一个 DataTemplate
,它可以为 GridView
的所有列共享,它的列是动态创建的(通过代码隐藏)。
我想将 DataTemplate
创建为 XAML 中的资源,而不是完全在代码隐藏中,但我不知道如何让绑定正常工作。
以下是我能想到的最接近的(但不起作用):
<DataTemplate x:Key="ListViewCellTemplate">
<TextBlock Text="Binding RelativeSource=RelativeSource AncestorType=x:Type GridViewColumn" />
</DataTemplate>
此模板分配为每一列的CellTemplate
,如下所示:
BindableDataTable table = this.DataContext as BindableDataTable;
foreach (BindableDataColumn c in table.Columns)
GridViewColumn col = new GridViewColumn();
col.Header = c.ColumnName;
col.CellTemplate = this.FindResource("ListViewCellTemplate") as DataTemplate;
v.Columns.Add(col);
【问题讨论】:
你能解决这个问题吗?我现在也有同样的问题:( 【参考方案1】:答案:
在您的资源中设置 DataTemplate
<UserControl.Resources>
<ResourceDictionary>
<DataTemplate x:Key="GridViewCellTemplateStyle">
<TextBlock Text="Binding">
<TextBlock.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="Binding DataContext.CommandDoubleClick, RelativeSource=RelativeSource FindAncestor, AncestorType=x:Type ListView"/>
</TextBlock.InputBindings>
</TextBlock>
</DataTemplate>
</ResourceDictionary>
</UserControl.Resources>
创建您的网格视图并使列继承此数据模板
<ListView>
<ListView.View>
<GridView>
<GridViewColumn Width="Auto" Header="Column1" CellTemplate="StaticResource GridViewCellTemplateStyle"/>
<GridViewColumn Width="Auto" Header="Column2" CellTemplate="StaticResource GridViewCellTemplateStyle"/>
<GridViewColumn Width="Auto" Header="Column3" CellTemplate="StaticResource GridViewCellTemplateStyle"/>
<GridViewColumn Width="Auto" Header="Column4" CellTemplate="StaticResource GridViewCellTemplateStyle"/>
</GridView>
</ListView.View>
</ListView>
此示例向您展示如何在 GridViewColumn 中创建可双击的行。 只需更改您认为合适的 DataTemplate
【讨论】:
【参考方案2】:我也遇到了同样的问题。
我希望能够将数据模板应用于不同的列,但将每列绑定到单独的数据字段。
<ListView ItemsSource="Binding" Name="listViewIMS" Grid.Row="1" Margin="0,0,0,4" FontSize="11" AlternationCount="2" SelectionMode="Extended">
<ListView.Resources>
<DataTemplate x:Key="Templ">
<TextBlock TextAlignment="Left" Text="Binding"/>
</DataTemplate>
<DataTemplate x:Key="Tempr">
<TextBlock Width="78" Margin="-6,0" TextAlignment="Right" Text="Binding" />
</DataTemplate>
<Style x:Key="HeaderStyleRight" TargetType="GridViewColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Right"/>
</Style>
<Style x:Key="HeaderStyleLeft" TargetType="GridViewColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Left"/>
</Style>
</ListView.Resources>
<ListView.View>
<GridView x:Name="gridViewInvoices">
<GridViewColumn Width="80" DisplayMemberBinding="Binding Document">
<GridViewColumnHeader Tag="docNo" Content="Document" />
</GridViewColumn>
<GridViewColumn Width="220" DisplayMemberBinding="Binding Customer">
<GridViewColumnHeader Tag="customer" Content="Customer"/>
</GridViewColumn>
<GridViewColumn Width="100" DisplayMemberBinding="Binding inv_date, StringFormat='dd/MM/yy'" >
<GridViewColumnHeader Tag="date" Content="Date" />
</GridViewColumn>
<GridViewColumn Width="100" DisplayMemberBinding="Binding inv_l_catalogue" >
<GridViewColumnHeader Tag="catalogue" Content="Item" />
</GridViewColumn>
<GridViewColumn Width="60" DisplayMemberBinding="Binding inv_l_qty" >
<GridViewColumnHeader Tag="qty" Content="Qty" />
</GridViewColumn>
<GridViewColumn Width="80" DisplayMemberBinding="Binding inv_l_price, StringFormat='0.00' " >
<GridViewColumnHeader Tag="unitPrice" Content="Price" />
</GridViewColumn>
<GridViewColumn Width="50" DisplayMemberBinding="Binding inv_l_per" >
<GridViewColumnHeader Tag="per" Content="Per" />
</GridViewColumn>
<GridViewColumn Header="Goods" CellTemplate ="StaticResource Tempr" HeaderContainerStyle="StaticResource HeaderStyleRight" Width="80" >
<!--GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="78" Margin="-6,0" TextAlignment="Right" Text="Binding inv_l_lgoods2, StringFormat='0.00'" />
</DataTemplate>
</GridViewColumn.CellTemplate>-->
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
【讨论】:
我认为您在这里遇到的问题是您不能同时使用DisplayMemberBinding
和CellTemplate
。我不明白为什么不,但这也给我带来了麻烦......以上是关于所有列的 WPF GridView 共享单元格模板的主要内容,如果未能解决你的问题,请参考以下文章