wpf listview gridview交换项目模板

发布于 2024-08-12 18:15:21 字数 652 浏览 9 评论 0原文

我有一个使用网格视图显示数据的列表视图。该列表显示网格视图的典型数据。大量数据以行和列的形式显示。但是,在某些行上,我在列中没有任何可显示的数据。我想更换该行的项目模板,并为整行显示另一个模板,该模板将显示文本的“空白行”或“空行”之类的内容,并且还设置整行而不是单个单元格的样式。

我一直在尝试使用这篇文章: http ://cloudstore.blogspot.com/2008/06/creating-custom-view-mode-that-supports.html

所以我有一个列表视图的自定义视图。基本上,我已经对 GridView 类进行了子类化,并且在PrepareItems 方法重写上,我可以选择要为特定行显示的新模板或默认模板。但是,当我尝试选择一个新模板时,gridview 将永远不会为该行渲染任何内容。如果我使用默认模板它仍然会呈现。我假设 GridView 的布局干扰了我要换出的行的模板设置。这是可能的还是我必须创建一个模仿 GridView 的自定义视图而不是 GridView 类的子类?我想保持专栏的原样,而不需要太多的返工。有什么建议或任何人有此类场景的经验吗?

I have a list view that is displaying data using the gridview. This list is displaying data typical of grid views. Lots of data displayed in rows and columns. However, on some rows I do not have any data to display, in the columns. I would like to swap out the item template for that row and display another template for the entire row that would display something like "blank row" or "empty row" for the text and also style the whole row not the individual cells.

I have been trying to use this post: http://cloudstore.blogspot.com/2008/06/creating-custom-view-mode-that-supports.html.

So I have a custom view for the list view. Basically, I have subclassed the GridView class and on the PrepareItems method override, I can select the new template that I want to display for a particular row or the default template. However, when I try to select a new template, the gridview will never render anything for that row. It will still render if I use the default template. I assume that the layout for a GridView is interfering with my setting of the template for the row I want to swap out. Is this possible or will I have to create a custom view that mimics the GridView and not subclass the GridView class? I would like to keep the columns the way they are without too much rework. Any suggestions or anyone have experience with this type of scenario?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷迭香的记忆 2024-08-19 18:15:21

您可以使用数据触发器。使用这样的代码(根据 SomeItemProperty 的值是否为 0 来切换模板):

<DataTemplate x:Key="MyItemTemplate">
    <StackPanel>
        <StackPanel Name="normalStackPanel">
            <!-- template for normal row -->
        </StackPanel>
        <StackPanel Name="emptyStackPanel" Visibility="Collapsed">
            <!-- template for empty row -->
        </StackPanel>
    </StackPanel>

    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding SomeItemProperty}" Value="0">
            <Setter TargetName="normalStackPanel" Property="Visibility" Value="Collapsed" />
            <Setter TargetName="emptyStackPanel" Property="Visibility" Value="Visible" />
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

希望有帮助。

You can use DataTriggers. Use code like this (switching templates depending on if value of SomeItemProperty is 0 or not):

<DataTemplate x:Key="MyItemTemplate">
    <StackPanel>
        <StackPanel Name="normalStackPanel">
            <!-- template for normal row -->
        </StackPanel>
        <StackPanel Name="emptyStackPanel" Visibility="Collapsed">
            <!-- template for empty row -->
        </StackPanel>
    </StackPanel>

    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding SomeItemProperty}" Value="0">
            <Setter TargetName="normalStackPanel" Property="Visibility" Value="Collapsed" />
            <Setter TargetName="emptyStackPanel" Property="Visibility" Value="Visible" />
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

Hope it helps.

梦初启 2024-08-19 18:15:21

好的,这就是我为解决问题所做的事情,但是这会导致我进行一些返工。我用项目控件替换了列表视图。所以我不再使用列表视图的GridView。相反,我定义了一个 GridViewColumnCollection,其中的列按照我想要的方式设置。

对于标题,我使用了 GridViewHeaderRowPresenter,其 columns 属性绑定到 GridViewColumnCollection。这会处理列标题。

在我的标题下方,我插入了绑定到我的项目列表的 ItemsControl。但是,我现在使用 DataTemplateSelector,它将根据项目及其属性交换模板。如果您不熟悉数据模板选择器,请转到此处:数据模板选择器

基本上,列表中项目的默认模板是 GridViewRowPresenter,其 columns 属性绑定到 GridViewColumnCollection。这使其与标题保持一致。但是,我仍然可以通过数据模板选择器将行的模板替换为其他内容。这解决了我的问题。

但是,我现在必须使用我设计的网格视图重新设计列表视图,并使用此方法。因此,这并非没有代价。

Okay here is what I did to solve my problem, however it will incur some rework on my part. I replaced with my listview with an items control. So I am not using the GridView of the list view anymore. Instead I defined a GridViewColumnCollection with my columns set up with how I wanted them.

For my header I used a GridViewHeaderRowPresenter with its columns property bound to GridViewColumnCollection. This takes care of the Column Headers.

Underneath my header I insert the ItemsControl which is bound to my items list. However, I now use a DataTemplateSelector that will swap the template out depending on the item and its properties. If you are unfamiliar with the DataTemplate Selector go here: data template selector

Basically, my default template for items in the list is a GridViewRowPresenter which its columns property is bound to GridViewColumnCollection. This keeps it in line with the headers. However, I can still swap out the template for a row to be something else through the Data Template Selector. And this solves my problem.

However, I now have to rework out of a listview with a gridview that I had styled, and use this method. So this does not come without some cost.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文