2014-05-01 95 views
0

所以我想添加一个数据网格和一个按钮到我的wpf窗体。WPF xaml - datagrid和按钮不能共存?

<Window x:Class="Database_Filler.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="Database Filler" Height="350" Width="557"> 

    <DataGrid x:Name="data" HorizontalAlignment="Left" VerticalAlignment="Top" Height="230" Width="520"/> 

    <!--<Button Content="Run Query" HorizontalAlignment="Left" Margin="468,294,0,0" VerticalAlignment="Top" Width="75" Click="Button1_Click"/>--> 

</Window> 

但每当我取消按钮我得到的错误:“属性‘内容’设置为超过一次。”

回答

2

WindowContentControl这样只能承载一个元件。如果你想主持更多的一个元素,那么你需要某种可以容纳许多孩子的某种Panel。您可以从几个实现像GridStackPanelWrapPanel等选择上取决于你想如何安排你的项目

<Window x:Class="Database_Filler.MainWindow" ...> 
    <StackPanel> 
     <DataGrid x:Name="data" HorizontalAlignment="Left" VerticalAlignment="Top" Height="230" Width="520"/> 
     <Button Content="Run Query" HorizontalAlignment="Left" Margin="468,294,0,0" VerticalAlignment="Top" Width="75" Click="Button1_Click"/> 
    </StackPanel> 
</Window> 

编辑

最常用的Panel类型:

  • Grid定义由列和行组成的灵活网格区域
  • DockPanel定义一个区域,您可以在其中相对于彼此水平或垂直排列子元素。
  • Canvas定义一个区域,您可以在其中使用相对于“画布”区域的坐标明确定位子元素。
  • StackPanel将子元素排列成可以水平或垂直定向的单行。
  • WrapPanel将子元素从左到右依次放置,将内容分解到包含框边缘的下一行。后续排序顺序发生从上到下或从右到左
  • UniformGrid提供一种方法来安排在网格中的内容,其中网格中的所有单元格的大小相同。
2

该窗口只能有一个子元素。你需要把它放在例如网格内或StackPanel中

<Window x:Class="Database_Filler.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Database Filler" Height="350" Width="557"> 

    <Stackpanel> 
     <DataGrid x:Name="data" HorizontalAlignment="Left" VerticalAlignment="Top" Height="230" Width="520"/> 

     <Button Content="Run Query" HorizontalAlignment="Left" Margin="468,294,0,0" VerticalAlignment="Top" Width="75" Click="Button1_Click"/> 
    </Stackpanel> 

</Window>