我可以添加哪些代码来使datagrid显示并不是使用objectdataprovider返回的数据集DefaultView的所有列(例如2个)?我用这个tutorial来编写这个工作代码。
以下是woking 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>
答案 0 :(得分:0)
在Datagrid中将Autogenerate列更改为false。
如果数据集返回3列Id,Name和Address,并且您想跳过Name然后在DataGrid.Columns中添加这些列并提供绑定路径。
试试这个:
<DataGrid ItemsSource="{Binding Source={StaticResource РОБОТ}}" AutoGenerateColumns="False" HorizontalAlignment="Left"
Margin="85,127,0,0" VerticalAlignment="Top" Height="133" Width="265" SelectionChanged="DataGrid_SelectionChanged_1">
<DataGrid.Columns>
<DataGridTextColumn Header="id" Binding="{Binding Path=Id}"/>
<DataGridTextColumn Header="address" Binding="{Binding Path=Address}"/>
</DataGrid.Columns>
</DataGrid>