0

我可以添加什么代码来使数据网格显示不是使用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>
4

1 回答 1

0

在 Datagrid 中将自动生成列更改为 false。

如果 Dataset 返回 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>
于 2013-09-30T11:37:58.007 回答