【问题标题】:items of a listview are not being displayed in wpf列表视图的项目未在 wpf 中显示
【发布时间】:2014-10-15 04:14:09
【问题描述】:

我正在尝试将项目添加到列表视图中,其中每个项目都有两个文本块,但由于某些原因项目没有显示。这是我的 XAML 代码。

<Window x:Class="testapp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="524" Width="856">
<Grid Background="Black">
    <!--<Image Height="44" HorizontalAlignment="Left" Margin="284,0,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="229" Source="/testapp;component/Images/Picture1.png" />-->
    <Grid Height="431" HorizontalAlignment="Left" Margin="0,55,0,0" Name="grid1" VerticalAlignment="Top" Width="266" Background="Black" Opacity="0">
        <ListView  Height="430" HorizontalAlignment="Left" Margin="3,1,0,0" x:Name="listView1" VerticalAlignment="Top" Width="260" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <TextBlock Text="{Binding Name}" Foreground="Green" />
                        <TextBlock Text="{Binding Source}" Foreground="Green" />
                    </WrapPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
           <!-- <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>-->
        </ListView>

    </Grid>
</Grid>
</Window>

这是我的 C# 代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace testapp
{

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        List<MyItems> items = new List<MyItems>();
        items.Add(new MyItems() { Name = "House Party", Source = " DLNA" });
        items.Add(new MyItems() { Name = "Outdoor Party", Source = " DLNA" });
        listView1.ItemsSource = items;


    }


}

public class MyItems
{
    public String Name { get; set; }
    public String Source { get; set; }

}
}

即使我将文本块绑定到名称和来源,它仍然没有显示。有人可以帮助我吗?提前致谢。

【问题讨论】:

    标签: wpf listview binding


    【解决方案1】:

    由于不透明度参数,它不显示。你已经定义了

    Opacity="0"
    

    在你的xml中

    请使其在 (0-1) 之间的范围内

    旧代码

    <Grid Height="431" HorizontalAlignment="Left" Margin="0,55,0,0" Name="grid1" VerticalAlignment="Top" Width="266" Background="Black" Opacity="0">
    

    更改为(新代码)

    <Grid Height="431" HorizontalAlignment="Left" Margin="0,55,0,0" Name="grid1" VerticalAlignment="Top" Width="266" Background="Black">
    

    【讨论】:

    • 这样做时,即使指定的背景颜色为黑色,网格背景颜色也会变为白色。
    • 如回答中所述,您需要将其(不透明度)设置为 0-1 之间的某个位置,并且为了您的样式,您需要创建适当的样式。根据我的理解,上述答案符合您的要求,不透明度参数无法处理样式
    猜你喜欢
    • 1970-01-01
    • 2016-08-31
    • 2021-01-26
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    相关资源
    最近更新 更多