您好,我使用Visual Studio 2019和WPF,我的目标是提供一个GUI,用户可以在其中选择要监视的机器变量。 目前,我的GUI上有2个Datagrid和3个按钮 我想从文件中读取数据,并在左侧的Datagrid [pic]中显示数据。
我怎样才能将数据插入正确的Grid中?我的开头是正确的吗?
文件中的数据示例:
ablauf 0x200003e4 u32
第一个是变量名,第二个是地址,最后一个是变量的大小,我只需要表中的参数名和地址即可。
我找到了100种将Datagrid与Table和SQL-Database连接的解决方案,但没有与数组和表连接的解决方案,因此当有人知道更好的方法时,请写给我。
为了更好地理解我的代码使用者XAML:
<Viewbox Margin="0,0,-8,-1">
<Grid Margin="0,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="400" Height="280">
<DataGrid Name="tableAllVar" ItemsSource ="{Binding}" VerticalAlignment="Top" HorizontalAlignment="Left" Height="220" Width="150" Margin="5,30,0,0" FontSize="6" ColumnHeaderHeight="15" AutoGenerateColumns="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Parameter" Binding="{Binding maschinenParameter}" Width="60" />
<DataGridTextColumn Header="Adresse" Binding="{Binding valueAdresse}" Width="40"/>
</DataGrid.Columns>
</DataGrid>
<DataGrid Name="tableChosenVar" ItemsSource ="{Binding}" VerticalAlignment="Top" HorizontalAlignment="Left" Height="220" Width="150" Margin="240,30,0,0" FontSize="6" ColumnHeaderHeight="15" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Parameter" Binding="{Binding maschinenParameter}" Width="60" />
<DataGridTextColumn Header="Value" Binding="{Binding maschinenValue}" Width="40"/>
</DataGrid.Columns>
</DataGrid>
<Button Name="buttonAddVar" HorizontalAlignment="Left" Margin="178,60,0,0" Width="40" Height="40" VerticalAlignment="Top">
<StackPanel>
<Image Source="C:\Users\mgleich\source\repos\Inspect\Inspect\Resources\Rechter_Pfeil_48.png"/>
</StackPanel>
</Button>
<Button Name="buttonDeleteVar" HorizontalAlignment="Left" Margin="178,110,0,0" Width="40" Height="40" VerticalAlignment="Top">
<StackPanel>
<Image Source="C:\Users\mgleich\source\repos\Inspect\Inspect\Resources\Löschen_48.png"/>
</StackPanel>
</Button>
<Button Name="buttonSaveVarList" HorizontalAlignment="Left" Margin="178,160,0,0" Width="40" Height="40" VerticalAlignment="Top">
<StackPanel>
<Image Source="C:\Users\mgleich\source\repos\Inspect\Inspect\Resources\schwarz_save_48.png"/>
</StackPanel>
</Button>
<Label Content="Alle Programmvariablen:" HorizontalAlignment="Left" Margin="5,5,0,0" VerticalAlignment="Top" FontSize="8" Height="20" Width="150"/>
<Label Content="Zu überwachende Variablen:" HorizontalAlignment="Left" Margin="240,5,0,0" VerticalAlignment="Top" FontSize="8" Height="20" Width="150"/>
</Grid>
</Viewbox>
还有我的addVar.cs:
public partial class addVar : Window
{
public addVar()
{
InitializeComponent();
}
private void Window_Activated(object sender, EventArgs e)
{
int zahler = 0;
System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\Users\mgleich\source\repos\Inspect\Variablenliste.txt"); //Erstellung Streamreader
while (zahler < 2)
{
string line = sr.ReadLine();
if (line != "" && line != null)
{
//create a new Row
string[] linearray = line.Split(' ');
//add the elements from linearray[0-2] into the new Row
}
else {
zahler++;
}
}
}//End Window_Activated
}
答案 0 :(得分:1)
使用两个公共string
属性创建一个类:
public class YourType
{
public string maschinenParameter { get; set; }
public string valueAdresse { get; set; }
}
然后您可以为文件中的每一行创建一个此类的实例,并将它们添加到您设置或绑定List<string>
的{{1}}的{{1}}上: / p>
ItemsSource