Multiple format types listview in C# and XAML

谁说我不能喝 提交于 2020-01-06 19:52:18

问题


I am creating a messaging application with a listview that displays message objects.

When a new user joins, I want to add (for example) "John joined" but I want it to be formatted differently than a message.

How can I achieve this?


回答1:


For this scenario, you can implement the ItemsControl.ItemTemplateSelector property of the ListView.

For example, create a class inherit from DataTemplateSelector first like this:

public class MessageDataTemplateSelecotr : DataTemplateSelector
{
    public DataTemplate NormalTemplate { get; set; }
    public DataTemplate JoinedTemplate { get; set; }
    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
    {
        var message = item as MessageModel;
        if (message.MessageType.ToString() == "Normal")
            return NormalTemplate;
        else
            return JoinedTemplate;
    }
}

Then you can use this selector for example like this:

<Page.Resources>
    <DataTemplate x:Key="NormalMessageTemplate">
        <TextBlock Text="{Binding Message}" FontSize="20" Foreground="Blue"/>
    </DataTemplate>
    <DataTemplate x:Key="JoinedMessageTemplate">
        <TextBlock Text="{Binding Message}" FontSize="15" Foreground="Red"/>
    </DataTemplate>
    <local:MessageDataTemplateSelecotr x:Key="MessageDataTemplateSelecotr" 
                                       NormalTemplate="{StaticResource NormalMessageTemplate}" 
                                       JoinedTemplate="{StaticResource JoinedMessageTemplate}"/>
</Page.Resources>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ListView ItemTemplateSelector="{StaticResource MessageDataTemplateSelecotr}" ItemsSource="{x:Bind listViewCollection}"/>
</Grid>

In the code behind just add data into the listViewCollection like this:

ObservableCollection<MessageModel> listViewCollection = new ObservableCollection<MessageModel>();
private enum _MessageType
{
    Normal,
    Joined
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    listViewCollection.Add(new MessageModel { Message = "hello world!", MessageType = _MessageType.Normal });
    listViewCollection.Add(new MessageModel { Message = "John joined", MessageType = _MessageType.Joined });
}

And my MessageModel class is like this:

public class MessageModel
{
    public string Message { get; set; }
    public Enum MessageType { get; set; }
}

The rendering image of this ListView is here:




回答2:


Have a look at the accepted answer on this question, ItemsControl with multiple DataTemplates for a viewmodel

You could use a CompositionCollection with two Datatemplates defined in your listview. One datatemple for normal messages, one for join messages.



来源:https://stackoverflow.com/questions/38276321/multiple-format-types-listview-in-c-sharp-and-xaml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!