【问题标题】:Why does no WPF ListView Theme work?为什么没有 WPF ListView Theme 工作?
【发布时间】:2015-01-24 07:55:43
【问题描述】:

我不是平面设计师,但我想要一个漂亮的应用程序。我在互联网上搜索了 WPF 主题,我发现了这些:
https://visualstudiogallery.msdn.microsoft.com/30e8b37d-01a0-4749-abcb-a5d7631e7646
主题适用于按钮和文本框以及许多控件,但不适用于 ListView。
这就是我的示例 ListView 在没有主题的情况下的外观:

这就是 Theme ShinyBlue 的外观(其他主题看起来相似):

我使用哪个主题并不重要(我下载了很多)。总是存在 ListView with Theme 不显示项目的问题。它只显示一些粗线而不是项目。
代码如下:

 <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
            mc:Ignorable="d" x:Class="SahaWpfTheme20131.TestWindow1"
            Title="Test Window" Width="250" Height="200"
            >
            <Grid>
                 <ListView  x:Name="ListView1" HorizontalAlignment="Left"  
                        VerticalAlignment="Top" Width="200" Height="150">
                    <ListViewItem Content="Coffee"/>
                    <ListViewItem Content="Tea"/>
                    <ListViewItem Content="Orange Juice"/>
                    <ListViewItem Content="Milk"/>
                    <ListViewItem Content="Iced Tea"/>
                    <ListViewItem Content="Mango Shake"/>
                </ListView>
            </Grid>
        </Window>

是我做错了什么还是很多主题都不能与 ListView 一起使用?我很困惑为什么这不起作用。

我使用了这里的 ListView 示例:http://www.c-sharpcorner.com/uploadfile/mahesh/listview-in-wpf/

【问题讨论】:

  • 嗨,我刚刚尝试了几个主题,看看你的意思似乎不起作用。试过 csetting ItemTemplate 但没有用。 ListBox 似乎适用于该主题,但您是否尝试过?

标签: wpf xaml listview themes


【解决方案1】:

当我创建一个带有列的ListView 和一个ItemsSource 时,它可以工作(但颜色搞砸了,可能是另一个问题):

    <ListView  x:Name="ListView1" HorizontalAlignment="Left"  
                    VerticalAlignment="Top" Width="200" Height="150">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn DisplayMemberBinding="{Binding A}" />
                    <GridViewColumn DisplayMemberBinding="{Binding B}" />
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>

在代码隐藏中(当然这不是正确的方法,但它是最短的):

        ListView1.ItemsSource = new[]
        {
            new { A = "qqq", B = "rrr" },
            new { A = "qqqq", B = "rrr" }
        };

我也让你的代码工作,但为此我更改了BureauBlack.xaml 中的ListViewItem 模板(正在使用的主题):

<GridViewRowPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="0,2,0,2" VerticalAlignment="Stretch" />

<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="0,2,0,2" VerticalAlignment="Stretch" />

现在显示您的代码列表中的内容。但是对于你想要做的事情,就像评论中建议的那样,你可能想要ListBox,而不是ListView

【讨论】:

  • 感谢您的工作和回答。根据您的建议,我将 GridViewRowPresenter 更改为 ContentPresenter,现在项目已正确显示。我使用上面的示例以尽可能少的代码来演示问题。在现实生活中,我的 ListView 代码更复杂,但显示这会使问题更难理解。您是否有一个很好的资源可以让我了解 GridViewRowPresenter、ContentPresenter 和可能的类似问题?
  • 对不起,我没有来源,我自己也不太了解这个。问题基本上是主题使用的GridViewRowPresenterGridView一起使用,而您原来的ListView不使用GridView
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-03
  • 2010-11-13
  • 1970-01-01
  • 1970-01-01
  • 2014-01-15
  • 1970-01-01
相关资源
最近更新 更多