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

WPF中如何绑定ListViewItem?实现点击ListView时下方TextBlock显示选中项文本

WPF ListViewItem绑定与选中项文本显示实现

Hi there! Let's fix your binding issue and get that TextBlock showing the selected item's text properly. The problem with your current code is that lol.SelectedItem refers to a ListViewItem object, which doesn't have a direct Text property — the actual text you want is stored inside the TextBlock that's set as the ListViewItem's content.

Here are two straightforward ways to resolve this:

方法1:直接绑定到选中项的内容属性

You can access the TextBlock's text directly by traversing the SelectedItem.Content.Text path. I've also added a FallbackValue to handle the case when no item is selected, which makes the UI more user-friendly:

<Window x:Class="WpfApp1.MainWindow"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <ListView x:Name="lol">
            <ListViewItem>
                <TextBlock Text="One"/>
            </ListViewItem>
            <ListViewItem>
                <TextBlock Text="Two"/>
            </ListViewItem>
            <ListViewItem>
                <TextBlock Text="Three"/>
            </ListViewItem>
        </ListView>
        <!-- 修正后的绑定:获取选中项的Content(即TextBlock)的Text属性 -->
        <TextBlock Text="{Binding Path=SelectedItem.Content.Text, ElementName=lol, FallbackValue='No item selected'}"/>
    </StackPanel>
</Window>

方法2:使用SelectedValue和SelectedValuePath(更规范)

This approach is cleaner and more scalable, especially if you plan to switch to dynamic data binding later. We'll tell the ListView exactly where to find the value we want using SelectedValuePath, then bind directly to SelectedValue:

<Window x:Class="WpfApp1.MainWindow"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <!-- 指定选中值的路径为Content.Text -->
        <ListView x:Name="lol" SelectedValuePath="Content.Text">
            <ListViewItem>
                <TextBlock Text="One"/>
            </ListViewItem>
            <ListViewItem>
                <TextBlock Text="Two"/>
            </ListViewItem>
            <ListViewItem>
                <TextBlock Text="Three"/>
            </ListViewItem>
        </ListView>
        <!-- 直接绑定到SelectedValue,获取目标文本 -->
        <TextBlock Text="{Binding Path=SelectedValue, ElementName=lol, FallbackValue='No item selected'}"/>
    </StackPanel>
</Window>

额外建议:用数据绑定替代硬编码项

If your data is dynamic or you want to follow WPF's best practices, consider using an ObservableCollection in your code-behind (or ViewModel) and binding it to the ListView's ItemsSource. This makes your code more maintainable:

In MainWindow.xaml.cs:

using System.Collections.ObjectModel;

namespace WpfApp1
{
    public partial class MainWindow
    {
        public ObservableCollection<string> Items { get; set; }
        public string? SelectedItem { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            Items = new ObservableCollection<string> { "One", "Two", "Three" };
            DataContext = this;
        }
    }
}

Then update the XAML:

<ListView ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}"/>
<TextBlock Text="{Binding SelectedItem, FallbackValue='No item selected'}"/>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 05:09:11