即使沒有項目,是否可以在gridview中繼續變更樣式?繼續WPF gridview變更
alt text http://i42.tinypic.com/xla5gm.jpg
正如你所看到的,最後一個項目之後,該模式將停止。
即使沒有項目,是否可以在gridview中繼續變更樣式?繼續WPF gridview變更
alt text http://i42.tinypic.com/xla5gm.jpg
正如你所看到的,最後一個項目之後,該模式將停止。
是的,WPF提供了一個相當優雅的方式來實現這一點,因爲它的模板機制允許你用任何你喜歡的方式填充GridView
中的未使用區域。
所有你需要做的就是修改ListView
模板漆的未使用部分與VisualBrush
通常由兩個GridViewItems
垂直堆疊(在一般情況下,這將是AlternationCount
GridViewItems
)。
唯一的複雜性是選擇在繪製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
這裏將是從給定的AlternationCount
和ItemsCount
在後臺代碼生成的僞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
中設置MinHeight
或Content
。或者,ContinueAlternation
可以在構建每個ListViewItem
時設置高度。
我把所有這些代碼都鍵入了我的頭頂,但它與我之前編寫和使用的代碼類似,所以它應該基本按原樣工作。
看看有什麼模式? – TFD 2010-03-17 06:52:22
深灰色,淺灰色,深灰色,淺灰色 – ErikTJ 2010-03-17 11:56:22
我非常喜歡那種風格!任何方式你可以發佈到pastebin.com併發送給我鏈接。 – Kredns 2010-03-21 05:11:24