如何更正显示数据网格的列和行?

时间:2018-08-02 12:32:50

标签: c# wpf xaml datagrid

我正在wpf mvvm中进行某些视图。我不知道如何修复列和行以正确显示我的数据网格。我想在名称下有一个文本框,在值下有一个文本框enter image description here 这是一张图片和xaml代码,有人可以帮我吗?

        <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <DataGrid ItemsSource="{Binding EnviromentVariables}" IsReadOnly="False" Margin="10" AutoGenerateColumns="False" Grid.ColumnSpan="4">
                            <DataGrid.Columns>
                                <DataGridTextColumn x:Name="name" Width="*" Header="Name" Binding="{Binding Name}" />
                                <DataGridTextColumn x:Name="value" Width="*" Header="Value" Binding="{Binding Value}" />
                            </DataGrid.Columns>
                        </DataGrid>
                        <StackPanel Orientation="Horizontal" Grid.Row="1">

                            <TextBox Grid.Row="1" Grid.Column="1" Width="{Binding ElementName =name}" />
                            <TextBox Grid.Row="1" Grid.Column="3" Width="{Binding ElementName= value}"/>

                            <Button Content="+" Grid.Column="2" Grid.Row="2" />
                            <Button Content="-" Grid.Column="3" Grid.Row="2" />
                        </StackPanel>
                    </Grid>

1 个答案:

答案 0 :(得分:0)

首先尝试删除StackPanel,因为即使使用Grid.RowGrid.Column属性,也不会有任何效果:

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <DataGrid ItemsSource="{Binding EnviromentVariables}" IsReadOnly="False" Margin="10" AutoGenerateColumns="False" Grid.ColumnSpan="4">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="name" Width="*" Header="Name" Binding="{Binding Name}" />
                <DataGridTextColumn x:Name="value" Width="*" Header="Value" Binding="{Binding Value}" />
            </DataGrid.Columns>
        </DataGrid>
        <TextBox Grid.Row="1" Grid.Column="0" Width="{Binding ElementName =name}" Grid.ColumnSpan="2" Margin="10,0,0,0"/>
        <TextBox Grid.Row="1" Grid.Column="2" Width="{Binding ElementName= value}" Grid.ColumnSpan="2" Margin="0,0,10,0"/>

        <Button Content="+" Grid.Column="2" Grid.Row="2" />
        <Button Content="-" Grid.Column="3" Grid.Row="2" />
</Grid>

请记住要调整文本框的边距。