根据项目状态更改ListView CellTemplate [英] Change ListView CellTemplate based on state of item
问题描述
我有一个ListView,它有一个ObservableCollection作为它的ItemsSource,并且它有几列.其中之一是状态"列,该列根据项目的当前状态显示不同的消息.当前,它是作为基本字符串实现的,尽管它可以正常工作,但远非美观或用户友好.我希望能够改变输出的种类,以更适合项目的状态.
I have a ListView that is has an ObservableCollection as its ItemsSource, and it has several columns. One of these is a State column which, depending on the current state of the item, shows a different message. Currently this is implemented as a basic string, and while it works it is far from pretty or userfriendly. I want to be able to vary the sort of output to more properly suit the state of the item.
我确实做了一些研究,并且知道我需要使用CellTemplate来影响显示,但是所有不同种类的模板都让我不知所措,以至于我不知道下一步该怎么做.
I did do some research and know that I need to use a CellTemplate to affect the display, but all the different sorts of templates simply overwhelm me to the point where I can't figure out where to go next.
我的代码(不包括很多其他的listview绒毛)如下:
My code (excluding lots of other listview fluff) is as follows:
<ListView Name="itemsListView" ItemsSource="{Binding Source={StaticResource listingDataView}}" IsSynchronizedWithCurrentItem="True">
...
<ListView.View>
<GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Item Information">
...
<GridViewColumn DisplayMemberBinding="{Binding Path=StatusMessage}" Width="283" Header="Status" HeaderContainerStyle="{StaticResource GVHeaderLeftAlignedStyle}" />
</GridView>
</ListView.View>
</ListView>
是的,这些项目已使用硬编码的状态消息"与其他实际上与代码相关的属性一起进行更新,从而在我的代码的其他地方造成了丑陋的重复. (是的,我知道这还不很漂亮,但是我也想改进它.)由于我并不是所有的创意人,所以该属性称为ItemState
.
Yes, the items have hardcoded 'Status Message' that get updated alongside other properties that are actually relevant for the code, causing ugly duplication elsewhere in my code. (And yes, I know this is far from pretty, but I want to improve this too.) That property would be called ItemState
as I am not all that creative.
因此,我的问题是:如何更改此列以使其具有最适合给定状态的显示?文字说明适用于许多州,但有些州篇幅较长,可能会从带有进度条的文字中受益,并且可能还需要一些时间.另一个州将从拥有可点击的超链接中受益.换句话说,我认为我至少需要3个不同的CellTemplates.
So, my question is: how can I vary this column to have the most suitable display for the given state? Textual descriptions will do for many states, but some are rather lengthy and might profit from a text with a progress bar besides it, and maybe some sort of time remaining. Another state would profit from having a clickable hyperlink. In other words, I think I need at least 3 different CellTemplates.
我意识到这是一个开放式的问题,主要是由于缺乏WPF经验的某人(= me)的设计错误而遭受的,但这正是我希望有经验的人可以直截了当的理由一些基本代码,然后再使事情变得比现在更糟. :)
I realize it is a rather open-ended question that largely suffers from the design mistakes of someone (=me) who has rather little experience with WPF, but that is exactly why I'm hoping someone experienced can set me straight with some basic code before I make an even worse mess of things than I have already. :)
推荐答案
您可以使用触发器来更改单元格的内容,例如
You can use triggers to change the content of the cell, e.g.
<GridViewColumn Header="Status">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ContentControl>
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding StateItem.HasError}" Value="True">
<Setter Property="ContentTemplate">
<Setter.Value>
<!-- Possibly create another contentcontrol which differentiates between errors -->
<DataTemplate>
<TextBlock Text="{Binding StateItem.Error}"
Foreground="Red"/>
</DataTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding StateItem.HasError}" Value="False">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Image Source="Images/Default.ico"/>
</DataTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
这种方式会使代码有些疯狂,尽管如果您进一步分支它,那是一种实现方式.
Code gets a bit crazy that way though if you branch it further but it's a way to do it.
设置程序应设置ContentTemplate
而不是Content
,否则显然不会创建新控件,并且仅一行显示正确的内容,因为该内容只能包含一个父母.
The setters should set the ContentTemplate
instead of the Content
, apparently otherwise no new controls may be created and only one row shows the proper content since the content can only have one parent.
这篇关于根据项目状态更改ListView CellTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!