You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF .NET 4.8中XAML文本无法显示的问题求助

WPF ListView GridViewColumn 单元格内容不显示问题解决

问题原因

  1. GridViewColumn未设置宽度:默认情况下GridViewColumn宽度为0,模板内容会被完全隐藏,这是最直接的原因。
  2. ListView无数据源:如果ListView未绑定任何数据集合,不会生成数据行,自然看不到单元格内容。
  3. 资源引用方式可选优化:使用DynamicResource虽可行,但模板是静态定义的,改用StaticResource更稳妥。

解决方案

1. 给GridViewColumn指定宽度

在GridViewColumn标签中添加与模板内Grid匹配的宽度值:

<GridViewColumn
    HeaderTemplate="{StaticResource UserHeaderTemplate}"
    CellTemplate="{StaticResource UserCellTemplate}"
    Width="190"/>

2. 给ListView绑定数据源(测试用)

在后台代码(UserListWindow.xaml.cs)中添加测试数据,确保ListView生成数据行:

public class User
{
    public string userName { get; set; }
}

public UserListWindow()
{
    InitializeComponent();
    // 绑定测试数据
    UserListView.ItemsSource = new List<User>
    {
        new User { userName = "测试用户1" },
        new User { userName = "测试用户2" }
    };
}

3. 可选:将DynamicResource改为StaticResource

模板在ListView资源中静态定义,使用StaticResource可避免动态资源查找的潜在问题:

CellTemplate="{StaticResource UserCellTemplate}"

修改后的完整XAML代码

<Window x:Class="ChattingClient.UserListWindow"
        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"
        xmlns:local="clr-namespace:ChattingClient"
        mc:Ignorable="d"
        Title="UserListWindow" Height="400" Width="250" Background="#BCBCBC">
    <Grid Margin="10,10,10,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="10*"></RowDefinition>
            <RowDefinition Height="1*"></RowDefinition>
        </Grid.RowDefinitions>
        <ListView x:Name="UserListView" Grid.Row="0" BorderBrush="White">
            <ListView.Resources>
                <DataTemplate x:Key="UserCellTemplate">
                    <Grid Width="190">
                        <TextBlock HorizontalAlignment="Center"
                                   Text="{Binding userName}"/>
                        <!-- 注释重叠的固定文本,避免覆盖绑定内容 -->
                        <!--<TextBlock HorizontalAlignment="Center"
                                   Text="123456"/>-->
                    </Grid>
                </DataTemplate>

                <DataTemplate x:Key="UserHeaderTemplate">
                    <Grid Width="190">
                        <TextBlock HorizontalAlignment="Center" Text="User"/>
                    </Grid>
                </DataTemplate>
            </ListView.Resources>

            <ListView.View>
                <GridView>
                    <GridViewColumn
                        HeaderTemplate="{StaticResource UserHeaderTemplate}"
                        CellTemplate="{StaticResource UserCellTemplate}"
                        Width="190"/>
                </GridView>
            </ListView.View>
        </ListView>
        <Grid Grid.Row="1" Margin="0,5,0,5">
            <Button x:Name="Chatting_btn" Click="Chatting_btn_Click"></Button>
        </Grid>
    </Grid>
</Window>

额外提示

  • 模板内的两个TextBlock会重叠显示,建议只保留一个(绑定字段或固定文本)。
  • 可开启Visual Studio的调试输出窗口(选择"调试"分类),查看绑定失败的详细提示。

内容的提问来源于stack exchange,提问作者Sam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 21:01:01