C#WPF绑定到数据库
大家好。如果有什么不清楚的话,请告诉我 我有dataGrid和TreeView。 我已经加载了数据库作为实体数据模型和一些表。 其中一个表“relation”应该显示给datagrid。但其(关系表)列依赖于系统、模型、功能和设备等其他表。数据网格中应有4列,其中包含这些系统、型号、功能和设备的名称。(如图1所示) 问题在于这一切如何表现。数据源工作不正常。。。见图2C#WPF绑定到数据库,c#,wpf,database,data-binding,binding,C#,Wpf,Database,Data Binding,Binding,大家好。如果有什么不清楚的话,请告诉我 我有dataGrid和TreeView。 我已经加载了数据库作为实体数据模型和一些表。 其中一个表“relation”应该显示给datagrid。但其(关系表)列依赖于系统、模型、功能和设备等其他表。数据网格中应有4列,其中包含这些系统、型号、功能和设备的名称。(如图1所示) 问题在于这一切如何表现。数据源工作不正常。。。见图2 <Grid DataContext="{StaticResource relationsViewSource}">
<Grid DataContext="{StaticResource relationsViewSource}">
<DataGrid AutoGenerateColumns="True" Name="gridInventory" HorizontalContentAlignment="Right" Margin="255,12,12,128" ItemsSource="{Binding}" />
<StackPanel Height="391" HorizontalAlignment="Left" Margin="10,10,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="239" DataContext="{StaticResource systemsViewSource}" >
<TreeView Height="391" Name="treeView1" Width="239" VerticalContentAlignment="Top" HorizontalAlignment="Left" VerticalAlignment="Top" ItemsSource="{Binding}" />
</StackPanel>
</Grid>
图1:
图2:
您需要在绑定上设置要显示的属性的路径的树视图 设备是一个集合,您需要在其中放置一个Listview或一些将显示集合的内容,然后在其上放置一个DataTemplate以显示所需内容,或者绑定到转换器以返回设备列表的静态字符串表示形式
或者让某人帮你做这一切,就像这里的情况一样,lol你将
树视图和一些数据网格
列绑定到一个对象,但不告诉WPF如何绘制对象。当WPF不知道如何绘制对象时,默认情况下,它使用文本块
将文本
绑定到对象的.ToString()
您需要设置ItemTemplate
来告诉WPF如何绘制各个对象,例如:
<TreeView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</TreeView.ItemTemplate>
<DataGrid.Resources>
<DataTemplate DataType="{x:Type local:Device}">
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</DataGrid.Resources>