数据填充 Datagrid,但未出现在 WPF 中
Data fills Datagrid, but doesn't appear in WPF
我正在创建一个字符串 List<>
,然后调用它来根据文本文档之间的差异填充数据网格。数据似乎正在填充数据网格,但实际上并没有显示出来。我知道这听起来令人困惑,但我玩过 background/foreground 颜色,看起来它们似乎根本不影响它。我能够让它与文本块一起使用,但后来我无法滚动。
编辑:我编辑了这个以试图提高我的声誉。短时间内跌了不少。我不确定这是我的代码格式还是问题的性质,但如果有人有建议我愿意听。
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 static List<int> DifferenceCounter = new List<int>();
public static List<int> IntDifferenceOccurance = new List<int>();
//public static List<string> Diff;
public ObservableCollection<CustomerInformation> CustomerInformationList { get; } = new ObservableCollection<CustomerInformation>();
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 ItemsSource="{Binding CustomerInformationList, RelativeSource={RelativeSource AncestorType=Window}}" Grid.RowSpan="4" HorizontalAlignment="Center" HorizontalContentAlignment="Center" AutoGenerateColumns="True" HorizontalScrollBarVisibility="Visible" Width="400" x:Name="Differences" Grid.Row="5" Grid.ColumnSpan="3" FontSize="16" Height="600" Grid.Column="1" VerticalAlignment="Top">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGrid}">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="LightYellow"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="BorderThickness" Value="0,0,1,2"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Width" Value="400"/>
</Style>
</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. -->
</DataGrid.Columns>
</DataGrid>
输出填充数据网格,但未显示。对于格式错误,我深表歉意。我很难纠正它们。这不应该是代码。
点击按钮之前
点击按钮后
首先,让我们创建一个可观察的集合,我们将使用它来填充 DataGrid。由于您的网格列绑定到一个名为 GetDiff
的 属性,并且 CustomerInformation
有一个同名的 属性,我猜您想用class。
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 使用我们创建的集合 属性。因为您在 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>
您正在将 string
添加到您的 DataGrid
但绑定到 GetDiff
。
方法“填充数据网格”
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"/>
我正在创建一个字符串 List<>
,然后调用它来根据文本文档之间的差异填充数据网格。数据似乎正在填充数据网格,但实际上并没有显示出来。我知道这听起来令人困惑,但我玩过 background/foreground 颜色,看起来它们似乎根本不影响它。我能够让它与文本块一起使用,但后来我无法滚动。
编辑:我编辑了这个以试图提高我的声誉。短时间内跌了不少。我不确定这是我的代码格式还是问题的性质,但如果有人有建议我愿意听。
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 static List<int> DifferenceCounter = new List<int>();
public static List<int> IntDifferenceOccurance = new List<int>();
//public static List<string> Diff;
public ObservableCollection<CustomerInformation> CustomerInformationList { get; } = new ObservableCollection<CustomerInformation>();
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 ItemsSource="{Binding CustomerInformationList, RelativeSource={RelativeSource AncestorType=Window}}" Grid.RowSpan="4" HorizontalAlignment="Center" HorizontalContentAlignment="Center" AutoGenerateColumns="True" HorizontalScrollBarVisibility="Visible" Width="400" x:Name="Differences" Grid.Row="5" Grid.ColumnSpan="3" FontSize="16" Height="600" Grid.Column="1" VerticalAlignment="Top">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGrid}">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="LightYellow"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="BorderThickness" Value="0,0,1,2"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Width" Value="400"/>
</Style>
</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. -->
</DataGrid.Columns>
</DataGrid>
输出填充数据网格,但未显示。对于格式错误,我深表歉意。我很难纠正它们。这不应该是代码。
点击按钮之前
点击按钮后
首先,让我们创建一个可观察的集合,我们将使用它来填充 DataGrid。由于您的网格列绑定到一个名为 GetDiff
的 属性,并且 CustomerInformation
有一个同名的 属性,我猜您想用class。
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 使用我们创建的集合 属性。因为您在 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>
您正在将 string
添加到您的 DataGrid
但绑定到 GetDiff
。
方法“填充数据网格”
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"/>