2012-12-04 69 views
1

在我的應用程序中,我使用ContentControl並動態使用自定義UserControl填充它,使用Content上的綁定綁定到FrameworkElement。 [編輯:添加示例代碼來顯示該問題]嵌套UserControl的自動調整大小不起作用

我創建了一個小MVVM項目重現該問題史蒂芬和Charleh的一些輸入之後。我的主窗口現在看起來像這樣:

<Window x:Class="ResizeExample.MainWindow" 
    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" 
    mc:Ignorable="d" 
    xmlns:localVM="clr-namespace:ResizeExample.ViewModels" 
    xmlns:local="clr-namespace:ResizeExample" 
    Title="ResizeExample" 
    WindowStartupLocation="CenterScreen" 
    Height="459" 
    Width="795"> 
<Window.Resources> 
    <localVM:MainWindowViewModel x:Key="Windows1ViewModel" /> 
</Window.Resources> 

<Grid DataContext="{StaticResource Windows1ViewModel}"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="50" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 
    <Canvas Grid.Row="0"> 
     <Menu DockPanel.Dock="Top"/> 
     <Label Content="Any Label on the right side" Canvas.Right="0" Canvas.Bottom="0"/> 
    </Canvas> 
    <Grid Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> 
     <ContentControl Name="ControlCanvas" Content="{Binding Path=externalView}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"/> 
    </Grid> 
</Grid> 

設置externalView工作正常,但是當窗口大小不會調整。在這裏我怎麼來實現它在視圖模型:

private FrameworkElement _externalView; 
    public FrameworkElement externalView 
    { 
     get { return this._externalView; } 
     set 
     { 
      if (this._externalView != value) 
      { 
       this._externalView = value; 
       RaisePropertyChanged(() => externalView); 
      } 
     } 
    } 

    public MainWindowViewModel() 
    { 
     externalView = new UserControl1(); 
    } 

UserControl包含TabControl(這應該調整)和其他一切是在TabControlTabControl包含一些標籤,文本框和按鈕(不應調整大小)和DataGrid(應調整大小)。目前整個TabControl已被最小化,因爲沒有設置大小,它不會調整大小。

<UserControl x:Class="ResizeExample.Views.UserControl1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="411" d:DesignWidth="805" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" > 
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" > 
    <TabControl HorizontalAlignment="Left" Name="tabControl1" VerticalAlignment="Top"> 
     <TabItem Header="Tab1" Name="Tab1"> 
      <Grid /> 
     </TabItem> 
     <TabItem Header="Tab2" Name="Tab2"> 
      <Grid> 
       <TextBox Height="23" HorizontalAlignment="Left" Margin="6,6,0,0" Name="textBox1" VerticalAlignment="Top" Width="357" /> 
       <Label Content="Input some data 1:" Height="28" HorizontalAlignment="Left" Margin="6,29,0,0" Name="label1" VerticalAlignment="Top" /> 
       <Label Content="Input some data 2:" Height="28" HorizontalAlignment="Left" Margin="6,57,0,0" Name="label2" VerticalAlignment="Top" /> 
       <Label Content="Input some data 3:" Height="28" HorizontalAlignment="Left" Margin="6,85,0,0" Name="label3" VerticalAlignment="Top" /> 
       <DataGrid AutoGenerateColumns="False" MinHeight="200" HorizontalAlignment="Left" Margin="6,113,0,0" Name="releaseNotesGrid" VerticalAlignment="Top" MinWidth="780" /> 
      </Grid> 
     </TabItem> 
    </TabControl> 
</Grid> 
</UserControl> 

我認爲控件會自動調整大小,但這是行不通的。雖然我是WPF和MVVM的新手,但可能是因爲我錯過了一些基本的東西。

我發現the following thread之後,我刪除了尺寸和添加的路線,但這並沒有解決我的問題。

回答

4

我現在解決它通過測試了一下。問題是在UserControlTabControlDataGrid,其中有未與舒展的工作路線。這是我現在的UserControl1,它爲我工作得很好:

<UserControl x:Class="ResizeExample.Views.UserControl1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"> 
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" > 
    <TabControl HorizontalAlignment="Stretch" Name="tabControl1" VerticalAlignment="Stretch"> 
     <TabItem Header="Tab1" Name="Tab1"> 
      <Grid /> 
     </TabItem> 
     <TabItem Header="Tab2" Name="Tab2" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch"> 
      <Grid> 
       <TextBox Height="23" HorizontalAlignment="Left" Margin="6,6,0,0" Name="textBox1" VerticalAlignment="Top" Width="357" /> 
       <Label Content="Input some data 1:" Height="28" HorizontalAlignment="Left" Margin="6,29,0,0" Name="label1" VerticalAlignment="Top" /> 
       <Label Content="Input some data 2:" Height="28" HorizontalAlignment="Left" Margin="6,57,0,0" Name="label2" VerticalAlignment="Top" /> 
       <Label Content="Input some data 3:" Height="28" HorizontalAlignment="Left" Margin="6,85,0,0" Name="label3" VerticalAlignment="Top" /> 
       <DataGrid AutoGenerateColumns="False" MinHeight="200" Margin="6,113,0,0" Name="releaseNotesGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinWidth="780" /> 
      </Grid> 
     </TabItem> 
    </TabControl> 
</Grid> 
</UserControl> 

感謝史蒂芬和Charleh點我到正確的方向。

1

Canvas不會自動調整其子內容到容器的大小。使用例如

嘗試代替Grid

+0

謝謝,我不知道畫布不會自動調整大小。不幸的是,它不能解決問題,但我現在添加了一個示例代碼。 – MikeR

2

除了史蒂文斯回答,您可能還需要設置ContentControl.HorizontalContentAlignmentContentControl.VerticalContentAlignmentStretchContentControl默認值分別爲LeftTop(據我記得)

否則即使ContentControl將填補父容器,內容將不會

編輯:這是爲ContentControl顯示默認值默認模板...

<Style TargetType="ContentControl"> 
    <Setter Property="Foreground" Value="#FF000000"/> 
    <Setter Property="HorizontalContentAlignment" Value="Left"/> 
    <Setter Property="VerticalContentAlignment" Value="Top"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ContentControl"> 
       <ContentPresenter 
        Content="{TemplateBinding Content}" 
        ContentTemplate="{TemplateBinding ContentTemplate}" 
        Cursor="{TemplateBinding Cursor}" 
        Margin="{TemplateBinding Padding}" 
        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 

+0

我向ContentControl添加了'Horizo​​ntalContentAlignment =「Stretch」VerticalContentAlignment =「Stretch」',但這並不能解決問題。我編輯了我的帖子並添加了完整的示例。 – MikeR