Itemscontrol拉伸silverlight

时间:2011-08-01 14:30:02

标签: silverlight silverlight-4.0

在我的itemsControl中,项目不占用用户控件的整个宽度。我使用DataTemplateSelector(手动编写的类)来选择模板的类型。

我检查了帖子Silverlight: Set Items Widths in ItemsControl to Stretch,但它不适合我。物品不会自动伸展和利用整个空间。请帮忙!提前致谢

<ItemsControl  ItemsSource ="{Binding}" Margin="0,5,0,0" HorizontalContentAlignment="Stretch" >
    <ItemsControl.ItemTemplate>
        <DataTemplate>                    
            <local:AddressFieldsTemplateSelector Content="{Binding}" x:Name="addressTemplateSelectorObject">
                <!-- TextBox template-->
                <local:AddressFieldsTemplateSelector.TextBoxDataTemplate>
                    <DataTemplate>                                
                        <Grid Margin="0,0,0,5" HorizontalAlignment="Stretch" >
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="0.5*" />
                                <ColumnDefinition Width=".1*" />
                                  <ColumnDefinition Width="*"/>

                            </Grid.ColumnDefinitions>
                            <TextBlock Text="{Binding AddressFieldName}"  Grid.Column="0" Style="{StaticResource DefaultTheme_TextBlockStyle}"/>
                            <TextBox Text="{Binding AddressFieldValue, Mode=TwoWay}" Grid.Column="2"  Style="{StaticResource TextBoxStyle}"  TextWrapping="NoWrap" MaxLength="50" HorizontalAlignment="Stretch" />
                        </Grid>
                    </DataTemplate>
                </local:AddressFieldsTemplateSelector.TextBoxDataTemplate>
            </local:AddressFieldsTemplateSelector>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

1 个答案:

答案 0 :(得分:0)

由于星形宽度,datatemplate中的项目将始终占用所有可用宽度。问题最有可能是itemscontrol本身。您确定itemscontrol未包含在StackPanel或自动宽度Grid列中。 ItemsControl的HorizontalAlignment是否设置为Left