我正在创建一个字符串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;
}
这是Datagrid的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,但不会显示。对于格式错误,我深表歉意。我很难让他们得到纠正。这不应该是代码。
单击按钮之前
单击按钮后
答案 0 :(得分:1)
首先,让我们创建一个可观察的集合,将其用于填充DataGrid。由于您的网格列绑定到名为GetDiff
的属性,并且CustomerInformation
具有该名称的属性,因此我猜您想用该类填充网格。
MainWindow.xaml.cs
// I don't know how you're populating this list. I'm guessing that happens in
// compareStringList() and then you call FillDataGrid().
private List<string> Diff;
public ObservableCollection<CustomerInformation> CustomerInformationList { get; }
= new ObservableCollection<CustomerInformation>();
public void FillDataGrid()
{
CustomerInformationList.Clear();
foreach (var diff in Diff)
{
CustomerInformation TempCust = new CustomerInformation();
TempCust.GetDiff = diff;
CustomerInformationList.Add(TempCust);
}
}
现在,让我们使用绑定来告诉您的DataGrid使用我们创建的collection属性。因为您在DataGrid上设置了AutoGenerateColumns="True"
,所以不需要自己创建列,但是有了绑定,所以我设置了AutoGenerateColumns =“ False”并包括了列定义。
<DataGrid
ItemsSource="{Binding CustomerInformationList, RelativeSource={RelativeSource AncestorType=Window}}"
AutoGenerateColumns="True"
HorizontalScrollBarVisibility="Visible"
Width="400"
x:Name="Differences"
Grid.Row="4"
Grid.ColumnSpan="3"
FontSize="16" Grid.Column="1"
>
<DataGrid.Resources>
<!-- Stuff omitted -->
</DataGrid.Resources>
<DataGrid.Columns>
<!--
Each row is one item from CustomerInformationList. That means each row is an instance of
CustomerInformation. CustomerInformation has a property named GetDiff, and here is how we
bind to that property.
-->
<DataGridTextColumn
Header="Difference On Lines"
Binding="{Binding GetDiff}"
FontSize="16"
Width="200"
/>
</DataGrid.Columns>
</DataGrid>
答案 1 :(得分:0)
您要将string
添加到DataGrid
,但绑定到GetDiff
。
方法“ FillDataGrid ”
CustomerInformation TempCust = new CustomerInformation();
TempCust.GetDiff = Diff[countLength];
Differences.Items.Add(TempCust.GetDiff); // <-- here you are adding string
// change it to "Differences.Items.Add(TempCust);" and it should work
XAML
<!-- here you are binding to "GetDiff" -->
<DataGridTextColumn Header="Difference On Lines" Binding="{Binding GetDiff}" FontSize="16" Width="200"/>