我動態地繪製到我的WPF網格內部的畫布上,但畫布或網格都不能正確拉伸。下面是我的一些代碼....WPF Grid不與窗口拉伸
<Window x:Class="WPFNimGame.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Game of Nim" Height="auto" Width="auto" PreviewKeyDown="Window_PreviewKeyDown_1" Name="Window">
<Grid Name="Grid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="33"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Border BorderBrush="Black" BorderThickness="10" Grid.RowSpan="2"/>
<!--<Grid.Background>-->
<!--<ImageBrush ImageSource=".\Properties\black-diamond-plate.bmp" -->
<!-- </Grid.Background>-->
<DockPanel VerticalAlignment="Top" Height="20" Width="{Binding ElementName=Window,Path=Width}" Grid.RowSpan="2" >
<Menu IsMainMenu="True" Margin="0" >
<MenuItem Name="File" Header="File" Click="File_Click_1">
<MenuItem Name="NewGame" Header="New Game" />
</MenuItem>
</Menu>
</DockPanel>
<Canvas Name="paintCanvas" Width="{Binding ElementName=Grid,Path=ActualWidth}" Height="{Binding ElementName=Grid,Path=ActualHeight}" MouseLeftButtonDown="paintCanvas_MouseLeftButtonDown" Grid.Row="1" >
<Border BorderBrush="Blue" BorderThickness="10" ></Border>
</Canvas>
</Grid>
您可以設置第1行的高度在RowDefinition但對於第1行的內容,您設定的高度,以網格的高度。在數學中,他們稱之爲過度定義。 – Paparazzi
取出所有對垂直/水平對齊,高度和寬度的引用。如果您希望對象始終位於特定的行/列中,則只需指定列,行,列跨度和行跨度。要擴展您的網格,請在定義旁邊放置一個*(例如33 *或僅*)。希望這可以幫助! –