7

我正在尝试创建一个DataTemplate可以为 a 的所有列共享的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);
            }
4

2 回答 2

3

回答:

在资源中设置 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

于 2015-07-27T00:15:43.170 回答
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>
于 2013-04-24T12:22:52.953 回答