我正在尝试创建一个DataTemplate
,可以为GridView
的所有列共享,其中包含动态创建的列(通过代码隐藏)。
我想在XAML中创建DataTemplate
作为资源,而不是完全在代码隐藏中,但我无法弄清楚如何使绑定正常工作。
以下是我能提出的最接近(但不起作用):
<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);
}
答案 0 :(得分:2)
答案:
在资源中设置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>
创建网格视图并使列继承此datatemplate
<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
答案 1 :(得分:1)
我遇到了同样的问题。
我希望能够将数据模板应用于不同的列,但每列都绑定到一个单独的数据字段。
<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>