Wpf 在itemscontrol中将线拉伸到Itemstemplate画布的宽度

Wpf 在itemscontrol中将线拉伸到Itemstemplate画布的宽度,wpf,xaml,itemscontrol,itemtemplate,itemspanel,Wpf,Xaml,Itemscontrol,Itemtemplate,Itemspanel,我有这个密码。由于某些原因,我无法让contentpresenter拉伸以填充画布的宽度。我的几次尝试都在xaml中被注释掉了 <ItemsControl ItemsSource="{Binding MarkerLocations, Mode=OneTime}" HorizontalContentAlignment="Stretch"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate>

我有这个密码。由于某些原因,我无法让contentpresenter拉伸以填充画布的宽度。我的几次尝试都在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>


我感觉我不理解ItemContainers的上下文。

如果要绑定到拉伸对象的宽度,则应绑定到
实际宽度

{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>


设置它的
背景
是一个有用的“布局调试”技巧,可以查看它是否真的在那里。从这里开始,您的一种方法应该起作用。

如果要绑定到拉伸对象的宽度,您应该绑定到
实际宽度

{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>


设置它的
背景
是一个有用的“布局调试”技巧,可以查看它是否真的在那里。从这里开始,您的一种方法应该会起作用。

我尝试过,但我的ContentPresenter的固定宽度仍然是3。画布宽度随窗口的宽度而变化(在控件运行时使用Snoop检查控件)。
容器样式中的绑定应为:
{binding ActualWidth,RelativeSource={RelativeSource AncestorType=canvas}
。非常好。我不需要对齐延伸,但我会将此标记为答案,因为我认为有人会需要它。@mydogisbox:你是对的,我测试它的“框架”有问题,因此编辑了我的答案。我可能正在挖掘一个旧线程,但我正在做一些类似的事情,让我惊讶的是xaml中有多么复杂。。。我是说所有这些东西,为什么一定要在那里?有关于这方面的好教程吗?我试过了,但我的ContentPresenters的固定宽度仍然是3。画布宽度随窗口的宽度而变化(在控件运行时使用Snoop检查控件)。
容器样式中的绑定应为:
{binding ActualWidth,RelativeSource={RelativeSource AncestorType=canvas}
。非常好。我不需要对齐延伸,但我会将此标记为答案,因为我认为有人会需要它。@mydogisbox:你是对的,我测试它的“框架”有问题,因此编辑了我的答案。我可能正在挖掘一个旧线程,但我正在做一些类似的事情,让我惊讶的是xaml中有多么复杂。。。我是说所有这些东西,为什么一定要在那里?有关于这方面的好教程吗?