2

我有一个小问题如何设置ItemsControlProperty Width。我Datatemplates用来填充我的ItemControl. 每个项目Grid都是Canvas.

当我ItemsControl在 aWindow中使用ActualWidth(and DesiredWidth) 时,总是0.0.

只有当我对 ItemsControl的Width属性进行硬编码时,它才有大小。Canvas但这远非完美。我宁愿从最大的项目中Control获取width(或)。DesiredWidth

这是一个简短的代码示例:

<ItemsControl ItemsSource="{Binding mySource}"
         ItemTemplate="{DynamicResource myTemplate}">

<ItemsControl.Resources>
    <DataTemplate DataType="{x:Type type:myType}"  x:Key="myTemplate">
        <Grid Width="Auto" Height="Auto">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition/>
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>

            <Rectangle Grid.Column="0"
                       Grid.ColumnSpan="2"
                       Grid.Row="1"
                       Height="1"
                       HorizontalAlignment="Stretch"
                       Fill="Blue"/>

            <TextBlock Grid.Row="0"
                       Grid.Column="1"
                       Margin="3"
                       Text="{Binding Text}"
                       FontWeight="Bold"
                       TextWrapping="Wrap"
                       TextAlignment="Left"/>

        </Grid>
    </DataTemplate>
    <v:PositionMultiConverter x:Key="PositionMultiConverter"/>
</ItemsControl.Resources>

<ItemsControl.ItemContainerStyle>
    <Style>
        <Setter Property="Canvas.Bottom">
            <Setter.Value>
                <MultiBinding Converter="{StaticResource ResourceKey=PositionMultiConverter}">
                    <Binding Path="Position"/>
                    <Binding Path="ActualHeight" ElementName="canvas"/>
                </MultiBinding>
            </Setter.Value>
        </Setter>
    </Style>
</ItemsControl.ItemContainerStyle>

<ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <Canvas x:Name="canvas" IsItemsHost="True" Width="20"/>
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>

4

0 回答 0