将控件添加到作为 ListView 的 ItemsPanelTemplate 的 Grid 中的空单元格
我有一个 WPF ListView,它有一个 Grid 作为 ItemsPanelTemplate。我根据项目的属性在正确的列和行中显示我的项目。但我想在网格的空单元格中放置一些控件。
这是我的代码和 xaml 的简化版本:
在资源中:
<ItemsPanelTemplate x:Key="TheTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
</Grid>
</ItemsPanelTemplate>
在 xaml 中:
<Controls:CustomListView ItemsSource="{Binding TheCollection}"
ItemsPanel="{DynamicResource TheTemplate}">
</Controls:CustomListView>
最后,在我的 CustomListView 中:
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
var viewModel = item as DomainObject;
if (viewModel != null)
{
element.SetValue(Grid.ColumnProperty, 1); //here I work with a converter, but this just simplifies it for StackOverflow
element.SetValue(Grid.RowProperty, 1);
}
}
注意:我知道我正在转换为 DomainObject,但请耐心等待。
这会给我带来一个网格,其中的项目位于正确的行和列中。但是,如果我想在空单元格中显示某些内容,例如“null”等文本,该怎么办?
我不能将其添加到我的模板中,因为这会导致应用程序崩溃,并表示 ItemsControl 将创建必要的控件。我尝试过在代码隐藏中访问网格/模板,但不太找到如何操作。也许我不应该使用ListView?也许还有其他/更好的解决方案?
I have a WPF ListView, which has a Grid as an ItemsPanelTemplate. I display my items in the correct column and row based on a property of the item. But I would like to put some controls in the empty cells of my grid.
This is a simplified version of my code and xaml:
In the resources:
<ItemsPanelTemplate x:Key="TheTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
</Grid>
</ItemsPanelTemplate>
In the xaml:
<Controls:CustomListView ItemsSource="{Binding TheCollection}"
ItemsPanel="{DynamicResource TheTemplate}">
</Controls:CustomListView>
Finally, in my CustomListView:
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
var viewModel = item as DomainObject;
if (viewModel != null)
{
element.SetValue(Grid.ColumnProperty, 1); //here I work with a converter, but this just simplifies it for StackOverflow
element.SetValue(Grid.RowProperty, 1);
}
}
NOTE: I know I'm casting to a DomainObject, but just bear with me, please.
What this will give me, is a grid with items in the correct row and column. But what if I want to display something in the empty cells, for example some text like 'null'?
I can't just add it to my template, because that crashes the application, saying the ItemsControl will create the necessary controls. I've tried accessing the grid/template in code-behind, but can't quite find how to. Maybe I shouldn't be using the ListView? Maybe there are other/better solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终做的是:
出于性能原因,我认为我会更改以下内容:
- 不是对项目集合进行迭代,而是对网格的所有可能的单元格进行迭代,添加一个控件(始终),并在存在相应项目时设置 DataContext。
这样做的原因是性能。填满网格大约需要 2 秒,我希望它能更快一些。
What I ended up doing was:
This is what I think I will change for performance reasons:
- Instead of iteration over the collection of items, iterate over all the possible cells of the Grid, add a Control (always), and set the DataContext if there is a corresponding item.
The reason to do this is performance. It takes about 2 secondes to fill the grid, and I'd like it to be faster.