2015-10-14 40 views
2

我有一個使用多個組樣式的數據網格。除了一個問題,這個工作非常好。偏移列標題留下背景顯示的空間

列標題從數據列偏移,因爲我在組風格中使用擴展器和其他項目容器。

我用下面的代碼解決了這個問題:

<!-- Extends the column header style to include a right click event handler and also to align the column headers with the data columns --> 
<DataGrid.ColumnHeaderStyle> 
    <Style BasedOn="{StaticResource NormalDataGridColumnHeaderStyle}" TargetType="DataGridColumnHeader"> 

     <!-- Offset the column headers to match the column data --> 
     <Setter Property="RenderTransform"> 
      <Setter.Value> 
       <TranslateTransform X="26" /> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</DataGrid.ColumnHeaderStyle> 

這個工作真的很好,但是我知道有一個小的,但沒有刺激性較小的問題,其中的列標題抵消了頭部區域留下一個小區域最左邊其中背景顯示了通過

enter image description here

如何延長最左邊的列標題來覆蓋這個空間?

與此類似,當一個滾動條出現有類似的差距abouve滾動條,我認爲可能是一個類似的問題

enter image description here

+0

的滾動條將出現在視圖的完整長度它用於。標題需要放在該容器的外面。至於標題,您需要自定義邊距和填充以強制其與左邊距和右邊距對齊。 –

+0

也許你可以用'ScaleTransform'替換你的'TranslateTransform',這取決於你的設計 – WiiMaxx

+0

也是一個測試代碼樣本:) – WiiMaxx

回答

0
  1. 左邊的保證金是由DataGridRowHeaderStyle引起

我將它設置爲綠色,以使其可見

01 DataGridRowHeaderStyle1的

<Style x:Key="DataGridRowHeaderStyle1" TargetType="{x:Type DataGridRowHeader}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type DataGridRowHeader}"> 
       <!-- I set Width to 50 to make border visible. Set Width="0" to make it disappear --> 
        <Border BorderThickness="1" BorderBrush="Green" Width="50" > 

這裏用法:

<DataGrid x:Name="datagridPersons" 
      RowHeaderStyle="{DynamicResource DataGridRowHeaderStyle1}" > 

(注:使DataGridRowHeaderStyle要創建:右鍵單擊數據網格/編輯其他模板/編輯DataGridRowHeaderStyle /創建副本)

爲了使邊距消失,

將寬度設置爲0,如在上面的XAML註釋中所寫的,並且th e邊際消失

  1. 當數據分組時,會再次出現更多邊距。

Datagrid, grouping, visible margins

展望與Visual Studio的可視化樹,您會看到:

Visual tree, debugging with Visual studio

這裏是有罪的!

(爲了看到這一點,請在圖形回調中設置斷點,請注意this或對數據網格的引用。 然後點擊放大鏡,在調試器)

Watching Visual tree in Visual studio debugger

我發現的唯一的解決辦法是在視覺樹檢索有罪ItemsPresenter並設置其利潤率爲0

最後一個問題是GroupItems是使用項目演示者動態創建的。

所以當添加GroupItems時需要刪除邊距。 所以我訂閱了CollectionViewSource分組事件

viewSource.View.GroupDescriptions.CollectionChanged += View_CollectionChanged; 


async void View_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
{ 
    // later ... 
    await Task.Delay(100); 
    RemoveMargins(); 
} 
private void RemoveMargins() 
{ 
    // For FindAllVisualDescendants() function, see my answer to that question : 
    // http://stackoverflow.com/questions/32736265/datagrid-templatecolumn-update-source-trigger-explicit-only-updates-first-row/32741477#32741477 
    var allItemsPresenter = datagridPersons.FindAllVisualDescendants() 
     .OfType<ItemsPresenter>() 
     .Where(elt => elt.Name == "ItemsPresenter") 
     .Where(elt => elt.Margin == new Thickness(5, 0, 0, 0)); 

    foreach (var itemsPresenter in allItemsPresenter) 
    { 
     itemsPresenter.Margin = new Thickness(0, 0, 0, 0); 
    } 
} 

,我不得不異步做到了。

這樣一來,它的工作原理:

Datagrid without border on Grouped Items

這裏是全碼:

http://1drv.ms/1VXx9qb

最佳編碼