<DataGrid.Columns>
<DataGridTextColumn Header="A" Width="150" Binding="{Binding A}"/>
<DataGridTextColumn Header="B" Width="100" Binding="{Binding B}"/>
<DataGridTextColumn Header="C" Width="100" Binding="{Binding C}"/>
<DataGridTextColumn Header="D" Width="100" Binding="{Binding D}"/>
<DataGridTextColumn Header="E" Width="100" Binding="{Binding E}"/>
<DataGridTextColumn Header="F" Width="100" Binding="{Binding F}"/>
<DataGrid.Columns/>
Now I want to take values from text file 1,2,3,4,5,6 in replace of A,B,C,D,E,F. How do I do that?
现在我想从文本文件1,2,3,4,5,6取代A,B,C,D,E,F中的值。我怎么做?
1 个解决方案
#1
0
To read a text file, you can use StreamReader:
要读取文本文件,可以使用StreamReader:
using (var reader = new StreamReader(File.OpenRead("c:/yourfile.txt"),
Encoding.GetEncoding("iso-8859-1")))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';'); // replace ';' by the your separator
string header1 = values[0];
string header2 = values[1];
string header3 = values[2];
//...
}
}
For DataGrid headers, you must bind each header to a property. You can try this implementation:
对于DataGrid标头,您必须将每个标头绑定到属性。你可以试试这个实现:
*.xaml
*的.xaml
<Grid Background="White" x:Name="Layout">
<DataGrid x:Name="grid" ItemsSource="{Binding MyCollection}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding FirstName}">
<DataGridTextColumn.Header>
<TextBlock DataContext="{Binding ElementName=Layout, Path=DataContext}" Text="{Binding Header1}"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
Code Behind *.cs
代码背后* .cs
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainViewModel();
Layout.DataContext = this.DataContext;
}
And ViewModel *.cs
和ViewModel * .cs
private string header1;
public string Header1
{
get
{
return header1;
}
set
{
this.header1 = value;
this.NotifyPropertyChanged("Header1");
}
}
Edit If you are not using MVVM, you can try this approach:
编辑如果您不使用MVVM,可以尝试以下方法:
*.cs
*的.cs
string Header1 = string.Empty;
using (var reader = new StreamReader(File.OpenRead("c:/yourfile.txt"),
Encoding.GetEncoding("iso-8859-1")))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';'); // replace ';' by the your separator
Header1 = values[0];
//...
}
}
grid.Columns[0].Header = Header1;
*.xaml
*的.xaml
<DataGrid.Columns>
<DataGridTextColumn Width="150" Binding="{Binding A}"/>
<DataGrid.Columns/>
#1
0
To read a text file, you can use StreamReader:
要读取文本文件,可以使用StreamReader:
using (var reader = new StreamReader(File.OpenRead("c:/yourfile.txt"),
Encoding.GetEncoding("iso-8859-1")))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';'); // replace ';' by the your separator
string header1 = values[0];
string header2 = values[1];
string header3 = values[2];
//...
}
}
For DataGrid headers, you must bind each header to a property. You can try this implementation:
对于DataGrid标头,您必须将每个标头绑定到属性。你可以试试这个实现:
*.xaml
*的.xaml
<Grid Background="White" x:Name="Layout">
<DataGrid x:Name="grid" ItemsSource="{Binding MyCollection}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding FirstName}">
<DataGridTextColumn.Header>
<TextBlock DataContext="{Binding ElementName=Layout, Path=DataContext}" Text="{Binding Header1}"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
Code Behind *.cs
代码背后* .cs
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainViewModel();
Layout.DataContext = this.DataContext;
}
And ViewModel *.cs
和ViewModel * .cs
private string header1;
public string Header1
{
get
{
return header1;
}
set
{
this.header1 = value;
this.NotifyPropertyChanged("Header1");
}
}
Edit If you are not using MVVM, you can try this approach:
编辑如果您不使用MVVM,可以尝试以下方法:
*.cs
*的.cs
string Header1 = string.Empty;
using (var reader = new StreamReader(File.OpenRead("c:/yourfile.txt"),
Encoding.GetEncoding("iso-8859-1")))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';'); // replace ';' by the your separator
Header1 = values[0];
//...
}
}
grid.Columns[0].Header = Header1;
*.xaml
*的.xaml
<DataGrid.Columns>
<DataGridTextColumn Width="150" Binding="{Binding A}"/>
<DataGrid.Columns/>