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

WPF主窗口搜索框联动另一窗口ListBox功能实现求助

WPF搜索框跨窗口匹配ListBox项实现方案

需求说明

在MainWindow的搜索框输入内容,点击按钮后,匹配SeeSoftware窗口中ListBox的项,并将匹配结果显示在主窗口内,原始列表项保留在SeeSoftware窗口中。

实现步骤与代码修改

第一步:重构SeeSoftware窗口,使用绑定数据源

将硬编码的ListBoxItem改为绑定集合,方便后续数据筛选与交互。

SeeSoftware.xaml

<Window x:Class="YourNamespace.SeeSoftware"
        Title="SeeSoftware" Height="700" Width="800" WindowStyle="None" MouseDown="Window_MouseDown">
    <Grid>
        <ListBox x:Name="Listbox" Foreground="White" FontSize="15" BorderBrush="Transparent"  
                 Background="Transparent" ItemsSource="{Binding SoftwareItems}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="20,0,0,0" HorizontalAlignment="Left">
                        <Image Height="30" Width="30" Source="{Binding IconPath}"/>
                        <Label VerticalAlignment="Center" Foreground="White" FontSize="18" Margin="20,0,0,0" 
                               Content="{Binding Name}" />
                        <TextBlock Margin="18,0,0,0" FontSize="18" VerticalAlignment="Center" >
                            <Hyperlink Foreground="White" NavigateUri="{Binding InstallUrl}" 
                                       RequestNavigate="Hyperlink_RequestNavigate">
                                Install
                                <Image Height="13" Source="/Resources/icons8_download_26px.png"/>
                            </Hyperlink>
                        </TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

SeeSoftware.xaml.cs

using System.Collections.ObjectModel;
using System.Windows;

namespace YourNamespace
{
    public partial class SeeSoftware : Window
    {
        // 公开数据源,供主窗口访问
        public ObservableCollection<SoftwareItem> SoftwareItems { get; set; }

        public SeeSoftware()
        {
            InitializeComponent();
            SoftwareItems = new ObservableCollection<SoftwareItem>();
            // 初始化软件数据
            SoftwareItems.Add(new SoftwareItem
            {
                Name = "Visual Studio code",
                IconPath = "/Resources/vscode.jpeg",
                InstallUrl = "https://az764295.vo.msecnd.net/stable/6261075646f055b99068d3688932416f2346dd3b/VSCodeUserSetup-x64-1.73.1.exe"
            });
            // 可添加更多软件项
            DataContext = this;
        }

        private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
        {
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(e.Uri.AbsoluteUri));
            e.Handled = true;
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
                DragMove();
        }
    }

    // 软件项实体类,封装信息
    public class SoftwareItem
    {
        public string Name { get; set; }
        public string IconPath { get; set; }
        public string InstallUrl { get; set; }
    }
}

第二步:修改MainWindow,添加结果展示区与搜索逻辑

在主窗口添加用于显示匹配结果的ListBox,实现按钮点击后的筛选逻辑。

MainWindow.xaml

<Window x:Class="YourNamespace.MainWindow"
        Title="Welcome"  Background="#FFD6D6D6" WindowStyle="None" ResizeMode="NoResize"
        WindowStartupLocation="CenterScreen" MouseDown="Window_MouseDown"
        AllowsTransparency="True" Height="600" Width="1050">
    <Grid>
        <!-- 搜索栏 -->
        <Grid Background="#2892b5" Margin="10,0,0,0" >
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition />
            </Grid.RowDefinitions>
            <Viewbox HorizontalAlignment="Left" Margin="22,4,0,0" VerticalAlignment="Center" Width="65">
                <Image  Source="/Resources/search_32px.png" Height="40" Width="70" />
            </Viewbox>
            <Viewbox HorizontalAlignment="Center" Margin="70,4,0,0" VerticalAlignment="Center" >
                <Label Content="Search for software" FontWeight="Bold" FontSize="20" Foreground="White"  
                       Grid.Row="0"  Width="286"/>
            </Viewbox>
            <TextBox Background="White" Foreground="Black" x:Name="search" FontSize="20" 
                     Grid.Row="1" Margin="37,0,36,38"/>
            <Button Grid.Row="1" Height="37" Width="37" Background="Transparent" BorderBrush="Transparent" 
                    VerticalAlignment="Top" HorizontalAlignment="Right" Click="SearchClick_Click">
                <Image Source="/View/icons8_right_50px_1.png"/>
            </Button>
        </Grid>

        <!-- 搜索结果展示区 -->
        <ListBox x:Name="SearchResultListBox" Margin="10,100,10,10" Background="#FFD6D6D6"
                 Foreground="Black" FontSize="15">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="20,0,0,0" HorizontalAlignment="Left">
                        <Image Height="30" Width="30" Source="{Binding IconPath}"/>
                        <Label VerticalAlignment="Center" Foreground="Black" FontSize="18" Margin="20,0,0,0" 
                               Content="{Binding Name}" />
                        <TextBlock Margin="18,0,0,0" FontSize="18" VerticalAlignment="Center" >
                            <Hyperlink Foreground="#2892b5" NavigateUri="{Binding InstallUrl}" 
                                       RequestNavigate="Hyperlink_RequestNavigate">
                                Install
                                <Image Height="13" Source="/Resources/icons8_download_26px.png"/>
                            </Hyperlink>
                        </TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

MainWindow.xaml.cs

using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Navigation;

namespace YourNamespace
{
    public partial class MainWindow : Window
    {
        // 持有SeeSoftware窗口实例,避免重复创建
        private SeeSoftware _seeSoftwareWindow;

        public MainWindow()
        {
            InitializeComponent();
            _seeSoftwareWindow = new SeeSoftware();
            // 如需默认打开SeeSoftware窗口,取消下方注释
            // _seeSoftwareWindow.Show();
        }

        private void SearchClick_Click(object sender, RoutedEventArgs e)
        {
            string searchText = search.Text.Trim().ToLower();
            if (string.IsNullOrEmpty(searchText))
            {
                SearchResultListBox.ItemsSource = null;
                return;
            }

            // 从SeeSoftware窗口获取数据源并筛选匹配项
            List<SoftwareItem> matchedItems = _seeSoftwareWindow.SoftwareItems
                .Where(item => item.Name.ToLower().Contains(searchText))
                .ToList();

            // 将匹配结果绑定到主窗口的ListBox
            SearchResultListBox.ItemsSource = matchedItems;
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
                DragMove();
        }

        private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
        {
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(e.Uri.AbsoluteUri));
            e.Handled = true;
        }
    }
}

关键说明

  • 使用ObservableCollection作为数据源,确保数据变更时UI自动更新
  • 通过实体类SoftwareItem封装软件信息,避免硬编码UI元素,提升可维护性
  • 主窗口持有SeeSoftware实例,直接访问其公开数据源进行筛选,实现跨窗口数据交互
  • 两个窗口的ListBox使用统一的DataTemplate,保证显示样式一致性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 21:25:25