2014-03-30 57 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>