2010-03-13 29 views
3

即使沒有項目,是否可以在gridview中繼續變更樣式?繼續WPF gridview變更

alt text http://i42.tinypic.com/xla5gm.jpg

正如你所看到的,最後一個項目之後,該模式將停止。

+0

看看有什麼模式? – TFD 2010-03-17 06:52:22

+0

深灰色,淺灰色,深灰色,淺灰色 – ErikTJ 2010-03-17 11:56:22

+0

我非常喜歡那種風格!任何方式你可以發佈到pastebin.com併發送給我鏈接。 – Kredns 2010-03-21 05:11:24

回答

2

是的,WPF提供了一個相當優雅的方式來實現這一點,因爲它的模板機制允許你用任何你喜歡的方式填充GridView中的未使用區域。

所有你需要做的就是修改ListView模板漆的未使用部分與VisualBrush通常由兩個GridViewItems垂直堆疊(在一般情況下,這將是AlternationCountGridViewItems)。

唯一的複雜性是選擇在繪製ScrollViewer的未使用部分時以哪種顏色開始。這是以Items.Count模計算的。解決方案是創建一個簡單的Control,進行此計算並將其用於我們的ListView模板中。爲了我的解釋,我將稱之爲「ContinueAlternation」。加入

ListView模板,將主要用local:ContinueAlternation控件的默認模板ScrollViewer低於使用DockPanel,像這樣:

<ControlTemplate TargetType="{x:Type ListView}"> 
    <Border BorderThickness="{TemplateBinding BorderThickness}" 
      BorderBrush="{TemplateBinding BorderBrush}" 
      Background="{TemplateBinding Background}" 
      SnapsToDevicePixels="True"> 
    <DockPanel> 
     <ScrollViewer DockPanel.Dock="Top" 
        Style="{DynamicResource {x:Static GridView.GridViewScrollViewerStyleKey}}" 
        Padding="{TemplateBinding Padding}"> 
     <ItemsPresenter SnapsToDevicePixels="True" /> 
     </ScrollViewer> 

     <local:ContinueAlternation 
     ItemContainerStyle="{TemplateBinding ItemContainerStyle}" 
     AlternationCount="{TemplateBinding AlternationCount}" 
     ItemsCount="{Binding Items.Count, 
          RelativeSource={RelativeSource TemplatedParent}}" /> 

    </DockPanel> 
    </Border> 
</ControlTemplate> 

ContinueAlternation控制將顯示爲塗有瓷磚VisualBrush一個Rectangle其中包含顯示虛擬行的ItemsControl,如下所示:

<ControlTemplate TargetType="{x:Type local:ContinueAlternation}"> 
    <Rectangle> 
    <Rectangle.Fill> 

     <VisualBrush TileMode="Tile" Stretch="None" 
        ViewPortUnits="Absolute" 
        ViewPort="{TemplateBinding ViewportSize}"> 

     <ItemsControl x:Name="PART_ItemsControl" 
         ItemsSource="{Binding}" /> 
     </VisualBrush> 
    </Rectangle.Fill> 
    </Rectangle> 
</ControlTemplate> 

DataContext這裏將是從給定的AlternationCountItemsCount在後臺代碼生成的僞ListViewItem數組:

public class ContinueAlternation 
{ 
    public Style ItemsContainerStyle ... // Declare as DependencyProperty using propdp snippet 
    public int AlternationCount ... // Declare as DependencyProperty using propdp snippet 
    public int ItemsCount ... // Declare as DependencyProperty using propdp snippet 

    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) 
    { 
    if(e.Property==ItemsContainerStyleProperty || 
     e.Property==AlternationCountProperty || 
     e.Property==ItemsCountProperty) 
    { 
     // Here is where we build the items for display 
     DataContext = 
     from index in Enumerable.Range(ItemsCount, 
             ItemsCount + AlternationCount) 
     select BuildItem(index % AlternationCount); 
    } 
    } 
    ListViewItem BuildItem(int alternationIndex) 
    { 
    var item = new ListViewItem { Style = ItemsContainerStyle }; 
    ItemsControl.SetAlternationIndex(item, alternationIndex); 
    return item; 
    } 

    protected override Size MeasureOverride(Size desiredSize) 
    { 
    var ic = (ItemsControl)GetTemplateChild("PART_ItemsControl"); 
    ic.Width = desiredSize.Width; 
    Size result = base.MeasureOverride(desiredSize); 
    ViewportSize = new Size(ic.DesiredSize); 
    return result; 
    } 
    public Size ViewportSize ... // Declare as DependencyProperty using propdp snippet 
} 

注意,這個相同的代碼可以與PropertyChangedCallback代替OnPropertyChanged被寫入。

你還需要做一些事情來確保空白行是所需的高度。最簡單的方法是在ItemsContainerStyle中設置MinHeightContent。或者,ContinueAlternation可以在構建每個ListViewItem時設置高度。

我把所有這些代碼都鍵入了我的頭頂,但它與我之前編寫和使用的代碼類似,所以它應該基本按原樣工作。