Я создаю строку List<>
, которая затем будет вызываться для заполнения сетки данных на основе различий между текстовыми документами. Данные, кажется, заполняют Datagrid, но на самом деле не отображаются. Я знаю, это звучит странно, но я играл с цветами фона и переднего плана, и кажется, что они даже не влияют на это. Я смог заставить это работать с текстовым блоком, но тогда я не мог прокрутить.
string sSelectedFile;
string sSelectedFolder;
public static List<String> txt1 = new List<string>();
public static List<String> txt2 = new List<string>();
public static List<String> Diff = new List<string>();
public MainWindow()
{
InitializeComponent();
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
Environment.Exit(0);
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
//fbd.Description = "Custom Description"; //not mandatory
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
sSelectedFolder = fbd.SelectedPath;
else
sSelectedFolder = string.Empty;
Textbox2.Text = sSelectedFolder;
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
OpenFileDialog choofdlog = new OpenFileDialog();
choofdlog.Filter = "txt files (*.txt)|*.txt";
choofdlog.FilterIndex = 1;
choofdlog.Multiselect = true;
if (choofdlog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
sSelectedFile = choofdlog.FileName;
else
sSelectedFile = string.Empty;
Textbox2.Text = sSelectedFile;
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
OpenFileDialog choofdlog = new OpenFileDialog();
choofdlog.Filter = "txt files (*.txt)|*.txt";
choofdlog.FilterIndex = 1;
choofdlog.Multiselect = true;
if (choofdlog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
sSelectedFile = choofdlog.FileName;
else
sSelectedFile = string.Empty;
Textbox3.Text = sSelectedFile;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
FillListTxt1();
FillListTxt2();
compareStringList();
}
public void FillListTxt1()
{
txt1.Clear();
try
{
var fileStream = new FileStream(Textbox2.Text, FileMode.Open, FileAccess.Read);
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
{
string line;
while ((line = streamReader.ReadLine()) != null)
{
txt1.Add(line);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
public void FillListTxt2()
{
txt2.Clear();
try
{
var fileStream = new FileStream(Textbox3.Text, FileMode.Open, FileAccess.Read);
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
{
string line;
while ((line = streamReader.ReadLine()) != null)
{
txt2.Add(line);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
public class CustomerInformation
{
public string GetDiff { get; set; }
}
public void compareStringList()
{
Diff.Clear();
string Text1;
string Text2;
string StillCounting = "Yes";
int IndexTxt1 = 0;
int IndexTxt2 = 0;
int Counter = 0;
Int32 length = txt1.Count;
Int32 length1 = txt2.Count;
while (StillCounting == "Yes")
{
if (length > IndexTxt1 & length1 > IndexTxt2)
{
Text1 = txt1[IndexTxt1];
Text2 = txt2[IndexTxt2];
if (Text1 != Text2)
{
//System.Windows.Forms.MessageBox.Show("There is a difference on line " + IndexTxt1.ToString());
string DifferencesInList = "There is a difference on line " + IndexTxt1.ToString();
Diff.Add(DifferencesInList);
IndexTxt1 = IndexTxt1 + 1;
IndexTxt2 = IndexTxt2 + 1;
Counter = Counter + 1;
}
else
{
IndexTxt1 = IndexTxt1 + 1;
IndexTxt2 = IndexTxt2 + 1;
}
}
else
{
StillCounting = "No";
}
}
if (Counter == 0)
{
System.Windows.Forms.MessageBox.Show("These are exactly the same");
}
FillDataGrid();
}
public void FillDataGrid()
{
Int32 length1 = Diff.Count;
int countLength = 0;
string Text2;
//Text2 = Diff[countLength];
while (length1 > countLength)
{
CustomerInformation TempCust = new CustomerInformation();
TempCust.GetDiff = Diff[countLength];
Differences.Items.Add(TempCust.GetDiff);
Differences.MinRowHeight = 30;
countLength = countLength + 1;
}
//Differences.DataContext = Diff;
}
Это код XAML для таблицы данных:
<DataGrid AutoGenerateColumns="True" HorizontalScrollBarVisibility="Visible" Width="400" x:Name="Differences" Grid.Row="4" Grid.ColumnSpan="3" FontSize="16" Grid.Column="1">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGrid}">
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="Background" Value="White"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="BorderThickness" Value="0,0,1,2"/>
<Setter Property="BorderBrush" Value="Black"/>
</Style>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="White"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="BorderThickness" Value="0,0,1,2"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</DataGrid.Resources>
<DataGridTextColumn Width="10"/>
<DataGridTextColumn Header="Difference On Lines" Binding="{Binding GetDiff}" FontSize="16" Width="200"/>
<DataGridTextColumn Width="10"/>
</DataGrid>
Выход заполняет Datagrid, но не отображается. Я прошу прощения за ошибки форматирования. Я испытываю трудности с их исправлением. Это не должно быть кодом.
До нажатия кнопки
![Before button click](https://i.stack.imgur.com/OOL5b.png)
После нажатия кнопки
![After button Click: Datagrid filled, but nothing showing up](https://i.stack.imgur.com/t6ARR.png)