如何解决使用TextBlock显示大文本文件的内容和创建日期在文件属性中找到,并更改TextBlock的边框颜色
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" textwrapping="Wrap"
Text="TextBox" FontFamily="Arial" VerticalAlignment="Top" Width="436" Name="FileNameTextBox"/>
<Button x:Name="browseButton" Content="browse a file" HorizontalAlignment="Left"
Margin="485,0" VerticalAlignment="Top" Width="121" Click="browseButton_Click"
RenderTransformOrigin="1.047,0.821" Height="40"/>
<TextBlock HorizontalAlignment="Left" Height="282" Margin="30,96,0"
textwrapping="Wrap" Text="TextBlock" FontFamily="Arial" VerticalAlignment="Top"
Width="703" Name="TextBlock1">
</TextBlock>
<Button x:Name="LoadButton" Content="Load" HorizontalAlignment="Left"
Margin="682,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编辑器的输出
我想要的是能够浏览和显示大文件的内容。我不在乎是显示整个文件还是显示其中的一个样本。
您需要从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);
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文件的“属性”窗口的右键单击->中。 (仅在可行的情况下。因为我不确定C#是否可以访问文件的属性窗口)
-
如何更改TextBlock对象的边框颜色并将其设置为白色。
在此先感谢您的帮助。
基于评论的更新
- 我已经在TextBlock周围添加了边框,如下所示:
<Border BorderThickness="3" BorderBrush="LightCyan">
<TextBlock HorizontalAlignment="Left" Height="282" Margin="30,0"
textwrapping="Wrap" Text="TextBlock" FontFamily="Arial" VerticalAlignment="Top"
Width="703" TextTrimming="CharacterEllipsis" Name="TextBlock1">
</TextBlock>
</Border>
但是突出显示的寄宿生会绕过我的网格,而不仅仅是文本块
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!");
}
解决方法
- 您可以使用TextBlock的
TextTrimming ="CharacterEllipsis"
属性。 - 您可以使用文件类
GetCreatonTime
的{{1}}方法获取文件的创建日期 - 您可以将TextBlock放入边框
File.GetCreationTime(path)
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。