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

WPF项目中如何从Window1获取文件并在MainWindow的RichTextBox显示?

项目信息

  • Windows Presentation Foundation(WPF)项目
  • 编程语言:C#

需求描述

实现从Window1让用户选择文件,读取文件内容后,在MainWindow的RichTextBox控件中展示该内容。

Window1(XAML)

<Window x:Name="window1" x:Class="Writer.Window1"
        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:Writer"
        mc:Ignorable="d"
        Title="New" Height="130" Width="600" WindowStyle="None" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize">
    <Grid Margin="0,0,0,-2">
        <Rectangle HorizontalAlignment="Center" Height="30" Stroke="Black" VerticalAlignment="Top" Width="600" Fill="Black"/>
        <Label Content="Open" HorizontalAlignment="Left" Margin="0,2,0,0" VerticalAlignment="Top" Foreground="White"/>
        <Button Content="X" Margin="579,5,10,0" VerticalAlignment="Top" Background="Black" Foreground="White" BorderBrush="Black" ToolTip="Exit" Focusable="False" IsTabStop="False" Click="Button_Click"/>
        <Label Content="选择文件路径" HorizontalAlignment="Left" Margin="0,35,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtFilePath" HorizontalAlignment="Left" Margin="5,61,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" IsTabStop="False"/>
        <Button x:Name="btnSelect" Content="选择" HorizontalAlignment="Left" Margin="5,84,0,0" VerticalAlignment="Top" BorderBrush="White" Background="#FFADADAD" Width="45" Click="BtnSelect_Click"/>
    </Grid>
</Window>

注:为文本框和选择按钮添加命名,方便后台代码关联;将路径提示文本改为中文。

主窗口(MainWindow,XAML)

<Window x:Name="MainWindow1" x:Class="Writer.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:Writer"
        mc:Ignorable="d"
        Title="Writer" Height="450" Width="800" WindowStyle="None" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="164*"/>
            <RowDefinition Height="83*"/>
            <RowDefinition Height="203*"/>
        </Grid.RowDefinitions>
        <Rectangle HorizontalAlignment="Center" Height="30" Stroke="Black" VerticalAlignment="Top" Width="800" Fill="Black"/>
        <Label Content="Writer" HorizontalAlignment="Left" Margin="0,2,0,0" VerticalAlignment="Top" Foreground="White"/>
        <Button Content="_" HorizontalAlignment="Left" Margin="765,5,0,0" VerticalAlignment="Top" Background="Black" Foreground="White" BorderBrush="Black" Focusable="False" ToolTip="最小化" IsTabStop="False" ClickMode="Press" Click="Button_Click_1"/>
        <Button Content="X" Margin="779,5,10,0" VerticalAlignment="Top" Background="Black" Foreground="White" BorderBrush="Black" ToolTip="退出" Focusable="False" IsTabStop="False" Click="Button_Click"/>

        <Menu Margin="0,30,0,114" Height="20">
            <MenuItem Header="文件">
                <MenuItem Header="新建">
                    <MenuItem.Icon>
                        <Image Source="/file-added.svg"/>
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="打开" Click="MenuItem_Click"/>
                <MenuItem Header="从服务器打开"/>
                <MenuItem Header="保存"/>
                <MenuItem Header="另存为"/>
                <MenuItem Header="关闭文件"/>
                <MenuItem Header="关闭文件夹"/>
            </MenuItem>
            <MenuItem Header="格式">
                <MenuItem Header="字体">
                    <MenuItem Header="字体"/>
                    <MenuItem Header="字体系列"/>
                    <MenuItem Header="字号"/>
                </MenuItem>
                <Separator/>
                <MenuItem Header="加粗"/>
                <MenuItem Header="倾斜"/>
                <MenuItem Header="下划线"/>
                <MenuItem Header="删除线"/>
            </MenuItem>
            <MenuItem Header="插入">
                <MenuItem Header="新站点"/>
            </MenuItem>
            <MenuItem Header="布局">
                
            </MenuItem>
            <MenuItem Header="视图">
                
            </MenuItem>
            <MenuItem Header="帮助"/>
        </Menu>
        <RichTextBox x:Name="RichTextBox1" Margin="0,50,0,0" BorderBrush="White" Cursor="Arrow" IsTabStop="False" Grid.RowSpan="3" FontFamily="Segoe UI" BorderThickness="0,0,0,0">
            <FlowDocument>
                <Paragraph>
                    <Run Text=""/>
                </Paragraph>
            </FlowDocument>
        </RichTextBox>
    </Grid>
</Window>

注:将菜单、按钮提示文本改为中文,符合使用习惯。

实现代码

1. Window1后台代码(Window1.xaml.cs)

using Microsoft.Win32;
using System.IO;
using System.Windows;

namespace Writer
{
    public partial class Window1 : Window
    {
        // 定义事件传递文件内容
        public event System.Action<string> FileContentSelected;

        public Window1()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void BtnSelect_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*";
            if (openFileDialog.ShowDialog() == true)
            {
                txtFilePath.Text = openFileDialog.FileName;
                string fileContent = File.ReadAllText(openFileDialog.FileName);
                // 触发事件传递内容
                FileContentSelected?.Invoke(fileContent);
                this.Close();
            }
        }
    }
}

2. MainWindow后台代码(MainWindow.xaml.cs)

using System.Windows;
using System.Windows.Documents;

namespace Writer
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            Window1 window1 = new Window1();
            // 订阅事件接收文件内容
            window1.FileContentSelected += Window1_FileContentSelected;
            window1.ShowDialog();
        }

        private void Window1_FileContentSelected(string content)
        {
            RichTextBox1.Document.Blocks.Clear();
            Paragraph paragraph = new Paragraph(new Run(content));
            RichTextBox1.Document.Blocks.Add(paragraph);
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 07:10:59