Вытяните линию по ширине Itemstemplate canvas в itemscontrol - PullRequest
1 голос
/ 05 ноября 2011

У меня есть этот код. По какой-то причине я не могу заставить растяжку содержимого растягиваться, чтобы заполнить ширину холста. Несколько моих попыток закомментированы в xaml.

<ItemsControl ItemsSource="{Binding MarkerLocations, Mode=OneTime}" HorizontalContentAlignment="Stretch">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="Canvas.Top" Value="{Binding}" />
            <Setter Property="Canvas.Left" Value="0" />
            <!--Setter Property="Width" Value="{Binding Path=Width, RelativeSource={RelativeSource FindAncestor, AncestorType=Canvas, AncestorLevel=1}}"/-->
        </Style>
    </ItemsControl.ItemContainerStyle>

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <!--Rectangle Stroke="Black" Height="2" Stretch="Fill"/-->
            <Line Stretch="Fill" X2="2" Y1="{Binding Mode=OneTime}" Y2="{Binding Mode=OneTime}" Stroke="Black" StrokeThickness="1"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

У меня такое ощущение, что я не понимаю контекст ItemContainters.

1 Ответ

4 голосов
/ 05 ноября 2011

Если вы хотите привязать к ширине растягиваемых объектов, вы должны привязать к ActualWidth:

{Binding ActualWidth, RelativeSource={RelativeSource AncestorType=Canvas}}

Редактировать: Возможно, это не такнеобходимый

У холстов есть привычка вообще не занимать места, если вы не скажете им:

  <ItemsControl ItemsSource="{Binding MarkerLocations, Mode=OneTime}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas Background="Red"
                        HorizontalAlignment="Stretch"
                        VerticalAlignment="Stretch"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
  </ItemsControl>

Установка Background - полезный трюк с отладкой макета дляпосмотрим, действительно ли его там.Оттуда один из ваших подходов должен работать.

...