我可以添加什么代码来使数据网格显示不是使用objectdataprovider返回的数据集DefaultView的所有列(例如全部2)?我使用本教程编写了这个工作代码。
以下是 XAML 代码:
<Window x:Class="wpf_2._0.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:wpf_2._0"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<!-- create an instance of our DataProvider class -->
<ObjectDataProvider x:Key="robot_data"
ObjectType="{x:Type local:robot_data}"/>
<!-- define the method which is invoked to obtain our data -->
<ObjectDataProvider x:Key="РОБОТ"
ObjectInstance="{StaticResource robot_data}"
MethodName="get_robot_data"/>
</Window.Resources>
<Grid >
<DataGrid ItemsSource="{Binding Source={StaticResource РОБОТ}}" AutoGenerateColumns="True" HorizontalAlignment="Left" Margin="85,127,0,0" VerticalAlignment="Top" Height="133" Width="265" SelectionChanged="DataGrid_SelectionChanged_1"/>
</Grid>
</Window>
解决方案
<Window x:Class="wpf_2._0.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:wpf_2._0"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<!-- create an instance of our DataProvider class -->
<ObjectDataProvider x:Key="robot_data"
ObjectType="{x:Type local:robot_data}"/>
<!-- define the method which is invoked to obtain our data -->
<ObjectDataProvider x:Key="РОБОТ"
ObjectInstance="{StaticResource robot_data}"
MethodName="get_robot_data"/>
</Window.Resources>
<Grid >
<DataGrid ItemsSource="{Binding Source={StaticResource РОБОТ}}" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="30,73,0,0" VerticalAlignment="Top" Height="133" Width="97">
<!-- create an instance of our DataProvider class -->
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=имя_робота}" Header="Имя робота"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>