单个选项卡中的多个DataGrid

时间:2018-05-06 09:57:33

标签: c# wpf

<TabControl>
    <TabItem Header="Order overzicht">
        <DataGrid HorizontalAlignment="Left" Height="22" VerticalAlignment="Top" Width="98"/>
        <DataGrid HorizontalAlignment="Left" Height="22" VerticalAlignment="Top" Width="98"/>
    </TabItem>
    <TabItem Header="Analyse">
        <DataGrid HorizontalAlignment="Left" Height="22" VerticalAlignment="Top" Width="98"/>
    </TabItem>
</TabControl>

将2个数据网格添加到单个tabitem时,会出现错误,指出“属性”内容“设置了多次”。 绕过这个的正确方法是什么?

看起来应该是这样的

It should look like this

2 个答案:

答案 0 :(得分:0)

您需要将DataGrid放置在某个面板中,例如网格有2行。面板可以包含多个元素。

<TabItem Header="Order overzicht">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <DataGrid Grid.Row="0" HorizontalAlignment="Left" Height="22" VerticalAlignment="Top" Width="98"/>
        <DataGrid Grid.Row="1" HorizontalAlignment="Left" Height="22" VerticalAlignment="Top" Width="98"/>
    </Grid>
</TabItem>

答案 1 :(得分:0)

除了询问的内容外,我还添加了一个按钮,可以在点击时添加行。这应该可以让您了解如何继续。

XAML:

<Window x:Class="TestingLayouts.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestingLayouts"
        mc:Ignorable="d"
        Title="Window2" Height="450" Width="800">
    <Grid>
        <TabControl Name="tabControl1" HorizontalAlignment="Left" Height="343" Margin="144,67,0,0" VerticalAlignment="Top" Width="477">
            <TabItem Header="Order overzicht">
                <DataGrid AutoGenerateColumns="False" Name="addDataItems" CanUserAddRows="True" ItemsSource="{Binding TestBinding}" Margin="0,0,0,50" >
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="OrderID" IsReadOnly="True" Binding="{Binding Path=OrderID}"  Width="80"></DataGridTextColumn>
                        <DataGridTextColumn Header="CustomerID" IsReadOnly="True" Binding="{Binding Path=CustomerID}"  Width="80"></DataGridTextColumn>
                        <DataGridTextColumn Header="EmployeeID" IsReadOnly="True" Binding="{Binding Path=EmployeeID}"   Width="80"></DataGridTextColumn>
                        <DataGridTextColumn Header="OrderDate" IsReadOnly="True"  Width="80"></DataGridTextColumn>
                        <DataGridTextColumn Header="RequiredDate" IsReadOnly="True"   Width="80"></DataGridTextColumn>
                    </DataGrid.Columns>

                </DataGrid>

            </TabItem>

            <TabItem Header="Analyse">
                <Grid Background="#FFE5E5E5">
                    <DataGrid HorizontalAlignment="Left" Height="100" Margin="10,0,0,0" VerticalAlignment="Top" Width="100"/>
                </Grid>
            </TabItem>
        </TabControl>
        <Button Content="Add new row" HorizontalAlignment="Left" Margin="41,164,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>


    </Grid>
</Window>

代码背后:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace TestingLayouts
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();
        }




      private void Button_Click_1(object sender, RoutedEventArgs e)
      {
         var data = new orderData { OrderID = "10248", CustomerID = "Test2", EmployeeID="5" };

         addDataItems.Items.Add(data);
      }
      public class orderData
      {
         public string OrderID { get; set; }
         public string CustomerID { get; set; }
         public string EmployeeID { get; set; }
      }
   }
}

<强>输出:

enter image description here