2014-03-30 58 views
1

我正在創建一個繼承自ItemsControl的WPF自定義控件。在我的控件的ControlTemplate中,我有一個ItemsPresenter。問題是我需要能夠在項目展示者中獲取內容以填充整個內容區域。在下面的代碼中,我有一個破解代碼的簡單例子,也是我想要完成的。 BTW將Horizo​​ntalAlignment和VerticalAlignment設置爲Stretch會導致內容水平擴展,但不是垂直擴展。ItemsControl阻止項目內容擴展以填充其容器

<Window x:Class="yada" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="768" Width="1024"> 
    <Grid> 
     <Grid.Resources> 
      <Style TargetType="Border" x:Key="stepBorder"> 
       <Setter Property="Background" Value="CadetBlue"></Setter> 
       <Setter Property="BorderBrush" Value="Green" ></Setter> 
       <Setter Property="BorderThickness" Value="2"></Setter> 
       <Setter Property="Padding" Value="25"></Setter> 
      </Style> 
     </Grid.Resources> 
     <TabControl Margin="0,20,0,0"> 
      <TabItem Header="Broken"> 
       <Border> 
        <ItemsControl> 
         <ItemsControl.Items> 
          <Control> 
           <Control.Template> 
            <ControlTemplate> 
             <Border Style="{StaticResource stepBorder}"> 
              <TextBlock Text="Border fills small region near the top"></TextBlock> 
             </Border> 
            </ControlTemplate> 
           </Control.Template> 
          </Control> 
         </ItemsControl.Items> 
        </ItemsControl> 
       </Border> 
      </TabItem> 

      <TabItem Header="Works"> 
       <Border> 
        <Control> 
         <Control.Template> 
          <ControlTemplate> 
           <Border Style="{StaticResource stepBorder}" > 
            <TextBlock Text="Border fills entire control"></TextBlock> 
           </Border> 
          </ControlTemplate> 
         </Control.Template> 
        </Control> 
       </Border> 
      </TabItem> 
     </TabControl> 
    </Grid> 
</Window> 

這裏是我的控件的一些代碼。我不能把它全部包括進來,那裏太多了。我認爲這是相關的部分,但是:

<Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type local:Wizard}"> 
       <Border Background="{TemplateBinding Background}" 
         BorderBrush="{TemplateBinding BorderBrush}" 
         BorderThickness="{TemplateBinding BorderThickness}"> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="*" x:Name="ContentRow" /> 
          <RowDefinition Height="{Binding ElementName= NavButtonPanel,Path=ActualHeight}"></RowDefinition> 
         </Grid.RowDefinitions> 
         <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" x:Name="ContentScrollViewer" MaxHeight="{Binding ElementName=ContentRowHeight, Path=ActualHeight}"> 
          <ItemsPresenter 
           x:Name="Presenter" 
           VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
           HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"> 
          </ItemsPresenter> 
         </ScrollViewer> 

         <Border Style="{TemplateBinding NavButtonPanelStyle}" Grid.Row="1" x:Name="NavButtonPanel"> 
         <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" > 
         </StackPanel> 
         </Border> 
        </Grid> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="ItemTemplate"> 
     <Setter.Value> 
      <DataTemplate></DataTemplate> 
     </Setter.Value> 
    </Setter> 

回答

2

像往常一樣,只是張貼在SO讓我的一半我的答案。

原來ItemsControl使用邪惡的堆棧面板作爲物品的容器。幸運的是,有一個叫做ItemsPanel的絕密屬性,可以讓你改變它。只需將其設置爲如下所示的網格,然後開始營業。

在我的情況下,我寫了這麼在我的風格添加該屬性設置器,它自ItemsControl繼承的自定義控制:

<Setter Property="ItemsPanel"> 
     <Setter.Value> 
      <ItemsPanelTemplate> 
       <Grid></Grid> 
      </ItemsPanelTemplate> 
     </Setter.Value> 
    </Setter>