Search code examples
c#wpftextblockopenfiledialog

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


I have the following XAML code that creates a browse file functionality of large txt files.

XAML code

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 file

<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>

Output of the WPF editor enter image description here

What I want is to be able to browse and show the content of a large file. I don't care if the whole file is shown or just a sample of it.

The execute I am executing can show only small files. Like the below:

You will need to load MaterialDesignThemes from Nuget packages to replicate the issue.

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!");
        }
    }
}

The code above what currently does is to present the content of a short txt file.

enter image description here

For example a large txt file that I want to present is like the following with 30K rows:

enter image description here

So what I want are the followings:

  1. Load the content of a larger file and if the content cannot fit in the TextBlock window then either present a short version of it or use a scroll bar to view the rest of its content.

  2. Display above the content of the txt file the Created On date which is found in Right Click-> the Properties window of the txt file. (Only if this is feasible. Because I am not sure if C# can access the properties window of a file)

  3. How can I change the border color of the TextBlock object and make it white.

Thank you in advance for any help.

UPDATES based on the comments

  1. I have add the boarder around my TextBlock like below:
            <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>

But the highlighted boarder goes around my Grid instead of only the textblock

enter image description here

  1. I used the GetCreationTime() property and indeed the creation time of the file was generated. Although, the previous text of the box was deleted and only the data value was shown. My code:
            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. The property TextTrimming="CharacterEllipsis" didn't seem to work. Even though it is correct, it did not solve my problem.

Solution

    1. You can use TextTrimming ="CharacterEllipsis" property of TextBlock.
    2. You can get file's creation date by using GetCreatonTime method of File class File.GetCreationTime(path)
    3. You can put TextBlock into Border <Border BorderThickness="1" BorderBrush="White"> <TextBlock ... /> </Border>.