基本的 ListView 样式 [英] Basic ListView Styling

查看:35
本文介绍了基本的 ListView 样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了文档并尝试了许多示例,但说实话,这些示例看起来像一团乱麻,似乎没有多大意义.

I have read the docs and tried many samples but to be honest the samples look like a big jumbled mess and don't seem to make much sense.

任何人都可以推荐关于如何在 XAML 中设置 ListView 控件样式的任何易于遵循的教程或文档吗?(无表情混合)

Can anyone recommend any easy to follow tutorials or docs on how to style a ListView control in XAML? (Without Expression Blend)

推荐答案

为 ListView 设置样式的两个主要常见技巧是设置项目的样式和更改列表框用来布置项目的容器类型.

The two main common tricks to styling a ListView are to style the items and change the kind of container the listbox uses to lay the items out.

为项目设置样式
这基本上意味着将 xaml 中的 ItemTemplate 设置为知道如何显示列表框 ItemsSource 内容的内容,通常使用绑定.

Styling an Item
This basically means setting the ItemTemplate in xaml to something that knows how to dispay the thing that is the content of the listbox's ItemsSource, typically using bindings.

例如,如果您有一个 ObservableCollection 绑定到客户定义为的列表框:

For example, if you have an ObservableCollection<Customer> bound to the listbox where customer is defined as:

public class Order
{
    public int Id { get; set; }
    public string OrderReference { get; set; }
    public string CustomerName { get; set; }
}

然后,您可以使用数据模板为项目设置样式,如下所示:

Then you might style the items with a data template as follows:

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding CustomerName}" />
                <TextBlock Text="{Binding OrderReference}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

一个基本的例子,但你明白了.

A basic example but you get the idea.

更改项目的布局方式
本质上,您可能不想要垂直列出项目的默认行为,因此您可以使用 ItemsPanel 属性将列表框内使用的容器控件更改为更合适的内容.例如,如果您的项目模板看起来像 Windows 资源管理器中大图标"视图中的项目,那么您可能希望列表框使用 WrapPanel 而不是 StackPanel(我很确定它是一个 StackPanel):

Changing how items are laid out
Essentially you might not want the default behaviour where items are listed vertically, so you can change the container control used inside the listbox to something more suitable using the ItemsPanel property. If, for example you had an item template that looked like an item from the "large icons" view in Windows Explorer, then you might want the listbox to use a WrapPanel rather than a StackPanel (I'm pretty sure it's a StackPanel):

<ListBox>
    <ListBox.ItemsPanel>
        <DataTemplate>
            <WrapPanel>
                <ContentPresenter />
            </WrapPanel>
        </DataTemplate>
    </ListBox.ItemsPanel>
</ListBox>

又是一个基本的例子.

我从内存中将所有这些代码写到 StackOverflow 中,如果其中有一些拼写错误或记错的部分,我们深表歉意.

I wrote all this code from memory into StackOverflow so apologies if there are a few typos or mis-remembered bits in there.

HTH.

这篇关于基本的 ListView 样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆