使用 TextBlock 显示大型文本文件的内容和创建日期(在文件属性中找到)并更改 TextBlock 的边框颜色

Use TextBlock to show the content and the date created (found in file properties) of a large text file and change the border color of the TextBlock

我有以下 XAML 代码创建大型 txt 文件的浏览文件功能。

XAML代码

Window x:Class="TestEnvironment.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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        xmlns:local="clr-namespace:TestEnvironment" 
        mc:Ignorable="d"
        Title="MainWindow" Height="600" Width="1080" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None">
    <StackPanel Background="WhiteSmoke">
        
        <!--Grid 1-->
        <Grid Height="40" ShowGridLines="True">
            <StackPanel HorizontalAlignment="Left" Margin="20 0">
                <ComboBox FontSize="15" Width="50" Foreground="#FFA2A2A2" SelectedIndex="0" VerticalAlignment="Center">
                    <ComboBoxItem IsSelected="False">EN</ComboBoxItem>
                    <ComboBoxItem IsSelected="True">GR</ComboBoxItem>
                </ComboBox>
            </StackPanel>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="20 0">
                <Button Content="FAQ" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="#FFA2A2A2" FontSize="15" FontWeight="Bold" VerticalAlignment="Center"/>
                <Button Content="CONTACT" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="#FFA2A2A2" FontSize="15" FontWeight="Bold" VerticalAlignment="Center"/>
                <Button Content="MY ACCOUNT" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="#FFA2A2A2" FontSize="15" FontWeight="Bold" VerticalAlignment="Center"/>
                <Button Background="{x:Null}" BorderBrush="{x:Null}" VerticalAlignment="Center" Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}">
                    <materialDesign:PackIcon Kind="Power" Foreground="#FFA2A2A2" Width="25" Height="25" />
                </Button>
            </StackPanel>
        </Grid>
        
        <!--Grid 2-->
        <Grid Height="100"  ShowGridLines="True">
            <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="10 0">
                <Button Uid="0" Width="150" Content="LOAD FILES" Height="50" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="#FF2196F3" Click="Button_Click"/>
                <Button Uid="1" Width="150" Content="FILES LAYOUT" Height="50" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="#FF2196F3" Click="Button_Click"/>
                <Button Uid="2" Width="150" Content="BUSINESS CHECKS" Height="50" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="#FF2196F3" Click="Button_Click"/>
            </StackPanel>
            <Grid x:Name="GridCursor" Width="150" Height="5" Background="#FF2196F3" HorizontalAlignment="Left" Margin="10 0"/>
        </Grid>
        
        <!--Grid 3-->
        <Grid x:Name="GridMain" Height="460" Background="LightGray" ShowGridLines="True">

            <TextBox HorizontalAlignment="Left" Height="40" Margin="30,13,0,0" TextWrapping="Wrap"   
                 Text="TextBox" FontFamily="Arial"  VerticalAlignment="Top" Width="436" Name="FileNameTextBox"/>
            <Button x:Name="BrowseButton" Content="Browse a file" HorizontalAlignment="Left"   
                Margin="485,13,0,0" VerticalAlignment="Top" Width="121" Click="BrowseButton_Click"   
                RenderTransformOrigin="1.047,0.821" Height="40"/>
            <TextBlock HorizontalAlignment="Left" Height="282" Margin="30,96,0,0"   
                   TextWrapping="Wrap" Text="TextBlock" FontFamily="Arial" VerticalAlignment="Top"  
                   Width="703" Name="TextBlock1">
            </TextBlock>
            <Button x:Name="LoadButton" Content="Load" HorizontalAlignment="Left"   
                Margin="682,13,0,0" VerticalAlignment="Top" Width="121" Click="BrowseButton_Click"   
                RenderTransformOrigin="1.047,0.821" Height="40"/>
        </Grid>

    </StackPanel>
</Window>

App.xaml 文件

<Application x:Class="TestEnvironment.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:TestEnvironment"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Blue.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Blue.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

WPF 编辑器的输出

我想要的是能够浏览和显示大文件的内容。我不在乎显示的是整个文件还是其中的一个示例。

我正在执行的execute只能显示小文件。像下面这样:

您需要从 Nuget 包加载 MaterialDesignThemes 才能重现该问题。

using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
using System.Data.SqlClient;
using System.Diagnostics;

namespace TestEnvironment
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>

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

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int index = int.Parse(((Button)e.Source).Uid);

            GridCursor.Margin = new Thickness(10 + (150 * index), 0, 0, 0);

            switch (index)
            {
                case 0:
                    GridMain.Background = Brushes.LightGray;
                    break;
                case 1:
                    GridMain.Background = Brushes.LightGray;
                    break;
                case 2:
                    GridMain.Background = Brushes.LightGray;
                    break;
            }
        }

        private void BrowseButton_Click(object sender, RoutedEventArgs e)
        {
            // Create OpenFileDialog
            Microsoft.Win32.OpenFileDialog openFileDlg = new Microsoft.Win32.OpenFileDialog();

            // Launch OpenFileDialog by calling ShowDialog method
            Nullable<bool> result = openFileDlg.ShowDialog();
            // Get the selected file name and display in a TextBox.
            // Load content of file in a TextBlock
            if (result == true)
            {
                FileNameTextBox.Text = openFileDlg.FileName;
                TextBlock1.Text = System.IO.File.ReadAllText(openFileDlg.FileName);
                Debug.WriteLine("Txt file contents!");
            }

            // Set filter for file extension and default file extension  
            openFileDlg.DefaultExt = ".txt";
            openFileDlg.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";

            Debug.WriteLine("Txt imported");

            // Set initial directory    
            openFileDlg.InitialDirectory = @"C:\Documents\";

            // Multiple selection with all file types    
            openFileDlg.Multiselect = true;

            Debug.WriteLine("End!");
        }
    }
}

上面的代码目前所做的是呈现一个简短的 txt 文件的内容。

例如,我要呈现的大型 txt 文件如下所示,有 30K 行:

所以我想要的是:

  1. 加载较大文件的内容,如果内容无法放入 TextBlock window,则显示其简短版本或使用滚动条查看其其余部分内容。

  2. 在txt文件的内容上方显示创建日期,可以在右键单击-> txt文件的属性window中找到。 (仅在可行的情况下。因为我不确定C#是否可以访问文件的属性window)

  3. 如何更改 TextBlock 对象的边框颜色并使其变为白色。

提前感谢您的帮助。

根据评论更新

  1. 我在我的 TextBlock 周围添加了边框,如下所示:
            <Border BorderThickness="3" BorderBrush="LightCyan">
            <TextBlock HorizontalAlignment="Left" Height="282" Margin="30,96,0,0"   
                   TextWrapping="Wrap" Text="TextBlock" FontFamily="Arial" VerticalAlignment="Top"  
                   Width="703" TextTrimming="CharacterEllipsis" Name="TextBlock1">
            </TextBlock>
            </Border>

但是突出显示的边框围绕着我的网格而不是仅文本块

  1. 我使用了 GetCreationTime() 属性 确实生成了文件的创建时间。虽然,之前框的文字被删除了,只显示了数据值。我的代码:
            if (result == true)
            {
                FileNameTextBox.Text = openFileDlg.FileName;
                TextBlock1.Text = System.IO.File.ReadAllText(openFileDlg.FileName);
                TextBlock1.Text = System.IO.File.GetCreationTime(openFileDlg.FileName).ToString();
                Debug.WriteLine("Txt file contents!");
            }
  1. 属性 TextTrimming="CharacterEllipsis" 似乎不起作用。虽然是对的,但是没有解决我的问题
  1. 您可以使用 TextTrimming ="CharacterEllipsis" 属性 的 TextBlock。
  2. 您可以使用 File class 的 GetCreatonTime 方法获取文件的创建日期 File.GetCreationTime(path)
  3. 您可以将 TextBlock 放入 Border <Border BorderThickness="1" BorderBrush="White"> <TextBlock ... /> </Border>