2011-03-03 27 views
0

我有一個問題,我找不出來。我希望我能夠解釋一切。WPF - 用戶控件高度尺寸不正確

基本上,我有一個用戶控件,我正在尋找作爲一種窗口模式對話框中使用。

<Grid> 
    <Rectangle Opacity=".75" Fill="White"/> 
    <Border Width="425" BorderBrush="LightGray" BorderThickness="2" CornerRadius="20,0,20,0" Padding="3"> 
     <Grid> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="auto"/> 
       <ColumnDefinition Width="*"/> 
       <ColumnDefinition Width="auto"/> 
       <ColumnDefinition Width="*"/> 
       <ColumnDefinition Width="auto"/> 
       <ColumnDefinition Width="*"/> 
      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="auto"/> 
       <RowDefinition Height="auto"/> 
       <RowDefinition Height="auto"/> 
       <RowDefinition Height="15"/> 
       <RowDefinition Height="auto"/> 
      </Grid.RowDefinitions> 
      <!-- First Name --> 
      <Label Grid.Row="0" Grid.Column="0" Content="First Name:"/> 
      <TextBox Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" Text="{Binding FirstName}" Margin="3"/> 
      <!-- Last Name --> 
      <Label Grid.Row="0" Grid.Column="3" Content="Last Name:"/> 
      <TextBox Grid.Row="0" Grid.Column="4" Grid.ColumnSpan="2" Text="{Binding LastName}" Margin="3"/> 
      <!-- Address --> 
      <Label Grid.Row="1" Grid.Column="0" Content="Address:"/> 
      <TextBox Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="5" Text="{Binding Address}" HorizontalAlignment="Stretch" Margin="3"/> 
      <!-- City --> 
      <Label Grid.Row="2" Grid.Column="0" Content="City:"/> 
      <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding City}" Margin="3"/> 
      <!-- State --> 
      <Label Grid.Row="2" Grid.Column="2" Content="State:"/> 
      <ComboBox Grid.Row="2" Grid.Column="3" ItemsSource="{Binding States}" SelectedValue="{Binding State}" Margin="3"/> 
      <!-- Zip Code --> 
      <Label Grid.Row="2" Grid.Column="4" Content="Zip Code:"/> 
      <TextBox Grid.Row="2" Grid.Column="5" Text="{Binding ZipCode}" Margin="3"/> 
      <Button Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="6" Width="100" HorizontalAlignment="Center" Content="Save" Command="{Binding SaveCustomerCommand}"/> 
     </Grid> 
    </Border> 
</Grid> 

我也有一個resourcedictionary包含一個datatemplate來連接這個usercontrol到它的viewmodel。

<DataTemplate DataType="{x:Type vm:CreateCustomerViewModel}"> 
    <view:CreateCustomerView/> 
</DataTemplate> 

最後,在主窗口視圖模型,創建控件的視圖模型的實例,並在主窗口視圖中,我使用一個ItemsControl,並結合它的ItemsSource屬性到控件的視圖模型的實例。

 <ItemsControl Height="600" Grid.Row="0" ItemsSource="{Binding CreateCustomerViewModel}" Grid.RowSpan="2" /> 

現在,我的問題是使用在主窗口中的ItemsControl,我已經嘗試了幾種不同的方式,但我不能得到控制是窗口的高度。我不確定我是不是應該使用itemscontrol,或者我做錯了什麼。很感謝任何形式的幫助。

回答

0

ItemsControl用於集合。默認情況下,它使用StackPanel來包含它的子元素,這些元素不允許在堆棧方向拉伸(默認情況下爲垂直)。對於單個項目,使用ContentControl(Button和Label之類的基礎)代替:

<ContentControl Height="600" Grid.Row="0" Content="{Binding CreateCustomerViewModel}" Grid.RowSpan="2" /> 
+0

非常有幫助,謝謝! – GenericUsername3043 2011-03-04 23:59:44

1

你可以嘗試以下方法:

把你的GridItemsControl

VerticalContentAlignment="Stretch"聲明ItemsControl

它不應該有任何區別,因爲它是默認設置,但是請聲明ItemsControlVerticalAlignment="Stretch"並嘗試刪除Height="600"

+0

不幸的是,似乎都沒有辦法。最終結果是一樣的。控件的寬度擴展很好,但高度停留在控件的高度,當我真的想要控件在窗口中間結束時,矩形佔據整個窗口。 – GenericUsername3043 2011-03-04 00:26:29