gpt4 book ai didi

wpf - 继续 WPF gridview 改动

转载 作者:行者123 更新时间:2023-12-02 02:34:28 25 4
gpt4 key购买 nike

即使没有项目,是否可以在 gridview 中继续更改样式?

alt text

如您所见,在最后一项之后,模式停止。

最佳答案

是的,WPF 提供了一种相当优雅的方式来实现这一点,因为它的模板机制允许您填充 GridView 中未使用的区域。随心所欲。

您需要做的就是修改 ListViewVisualBrush 绘制未使用部分的模板通常由两个 GridViewItems 组成垂直堆叠(一般情况下为 AlternationCount GridViewItems )。

唯一复杂的是在绘制 ScrollViewer 的未使用部分时选择以哪种颜色开始.这被计算为 Items.CountAlternationCount .解决方案是创建一个简单的 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控件将显示为 Rectangle用瓷砖画 VisualBrush包含 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这里将是一个虚拟数组 ListViewItem从给定的 AlternationCount 在代码隐藏中生成和 ItemsCount :

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 .

您还需要做一些事情来确保空白行的高度符合要求。最简单的方法是设置 MinHeightContent在你的ItemsContainerStyle .或者 ContinueAlternation可以在构造每个 ListViewItem 时设置高度.

我在脑海中输入了所有这些代码,但它与我之前编写和使用的代码相似,因此它应该基本上可以按原样工作。

关于wpf - 继续 WPF gridview 改动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2439779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com