1

私はこのコードを持っています。何らかの理由で、コンテンツプレゼンターをキャンバスの幅全体に拡大することができません。私の試みのいくつかは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のコンテキストを理解していないように感じます。

4

1 に答える 1

4

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は、実際にそこにあるかどうかを確認するための便利な「レイアウトデバッグ」トリックです。そこから、アプローチの1つが機能するはずです。

于 2011-11-04T22:34:18.880 に答える