none
wpf中DockPanel与Grid嵌套问题 RRS feed

  • 问题

  • 初学wpf,请教为什么在DockPanel里面嵌套Grid的时候,Grid不能填满DockPanel呢?下面是代码:

    <Window x:Class="Test.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Window1" Height="600" Width="600" WindowState="Maximized">
      <Window.InputBindings>
        <KeyBinding Command="ApplicationCommands.Save" Key="F3" Modifiers="Control"></KeyBinding>
        <KeyBinding Command="ApplicationCommands.Open" Key="F2" Modifiers="Control"></KeyBinding>
        <KeyBinding Command="ApplicationCommands.SaveAs" Key="F4" Modifiers="Control"></KeyBinding>
      </Window.InputBindings>
      <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Save" Executed="CommandBinding_Executed_Save" CanExecute="CommandBinding_CanExecute_Save"></CommandBinding>
        <CommandBinding Command="ApplicationCommands.Open" Executed="CommandBinding_Executed_Open" CanExecute="CommandBinding_CanExecute_Open"></CommandBinding>
      </Window.CommandBindings>
      
      <Window.Resources>
        <Style x:Key="topMenu" TargetType="{x:Type Menu}">
        </Style>
        <Style x:Key="topMenuItem" TargetType="{x:Type MenuItem}">
          <Setter Property="FontSize" Value="14"></Setter>
        </Style>
      </Window.Resources>
    
      <DockPanel HorizontalAlignment="Stretch" Name="dockPanel1" VerticalAlignment="Stretch" LastChildFill="True">
        <Menu Height="26" HorizontalAlignment="Stretch" Name="menuMenu" VerticalAlignment="Top" Width="auto" Style="{StaticResource topMenu}" DockPanel.Dock="Top">
          <MenuItem Name="menuFile" Header="文件" Style="{StaticResource topMenuItem}">
            <MenuItem Name="menuOpenProject" Header="打开" Command="ApplicationCommands.Open" ></MenuItem>
            <MenuItem Name="menuSaveProject" Header="保存" Command="ApplicationCommands.Save"></MenuItem>
            <MenuItem Name="menuSaveAsProject" Header="另存为..." Command="ApplicationCommands.SaveAs"></MenuItem>
          </MenuItem>
          <MenuItem Name="menuSelect" Header="选择" Style="{StaticResource topMenuItem}" />
          <MenuItem Name="menuTool" Header="工具" Style="{StaticResource topMenuItem}" />
          <MenuItem Name="menuSet" Header="设置" Style="{StaticResource topMenuItem}" />
          <MenuItem Name="menuWindow" Header="窗口" Style="{StaticResource topMenuItem}" />
        </Menu>
        
      <Grid HorizontalAlignment="Stretch" VerticalAlignment="Top">
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="30*"></ColumnDefinition>
          <ColumnDefinition Width="70*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
    
        <TreeView Grid.Row="0" Grid.Column="0">
            <TreeViewItem>
              <Label>adf</Label>
            </TreeViewItem>
          </TreeView>
        <GridSplitter Name="gridSplitter1" Width="5"></GridSplitter>
        <TabPanel Grid.Row="0" Grid.Column="1" >
            <TabItem>
              <Label>bbbbbbbbb</Label>
            </TabItem>
          </TabPanel>
    
      </Grid>
      
        <StatusBar Height="23" Name="statusBar1" DockPanel.Dock="Bottom" VerticalAlignment="Bottom" Background="Gray" HorizontalAlignment="Left"></StatusBar>
      </DockPanel>
      
    </Window>
    
    
    开发语言:C#
    
    开发工具:VS2008
    
    2010年10月1日 18:04

答案

  • 將Grid放最後不就填滿了麼?要設置DockPanel.Dock="Bottom"控件不可以放在其他內容之後,只能在top之後!

    • 已标记为答案 Alan.Deng 2010年10月13日 8:31
    2010年10月13日 8:30