2013-04-11 38 views
5

我想創建一個可以爲GridView的所有列共享的DataTemplate WPF GridView的共享單元模板,它有它的動態創建的列(通過代碼隱藏)。所有列

我想創建DataTemplate作爲XAML中的資源,而不是完全在代碼隱藏中,但我無法弄清楚如何讓綁定正常工作。

以下是我能想出最接近(但不工作):

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

真的,無人問津? – ChandlerPelhams 2013-04-11 20:08:42

+0

你能解決這個問題嗎?我現在有同樣的問題:( – Fabian 2016-02-22 08:47:44

回答

1

我:

<DataTemplate x:Key="ListViewCellTemplate"> 
     <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type GridViewColumn}}}" /> 
    </DataTemplate> 

此模板如下分配爲每列的CellTemplate m有完全相同的問題。

我希望能夠將數據模板應用到不同的列,但必須綁定到一個單獨的數據字段中的每個列。

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

我假設你有這裏的問題是,你不能用'DisplayMemberBinding'和'CellTemplate'在一起。我不爲什麼不理解,但它引起了我的麻煩了。 .. – CJBrew 2016-03-08 13:11:09

0

答:

設置一個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中,你認爲合適