DataTemplate 为列表中的第一项提供正确的结果,但为 WPF 中的第二项提供错误的结果

发布于 2024-12-13 16:50:19 字数 707 浏览 0 评论 0原文

我的控件中有以下项目:

<Canvas Name="canvasTrack" Height="{Binding CanvasHeight}" Width="{Binding CanvasWidth}"> 
   <ItemsControl  
         ItemsSource="{Binding Path=CurrentSegments}" 
         ItemTemplate="{StaticResource BegSegmentTemplate}">
    </ItemsControl>
</Canvas>

以及以下 DataTemplate

<DataTemplate x:Key="BegSegmentTemplate">
    <Ellipse Height="10" Width="10" Margin="{Binding EntryPoint}" Fill="Black" HorizontalAlignment="Center" />
</DataTemplate>

然后我将其绑定到其中包含两个项目的 ObservableCollection。当我运行该程序时,会出现两个省略号。第一个位于画布上的正确位置,但第二个位于“随机”位置。我检查了绑定的号码,一切正常。我可能会错过什么?

I have the following Item in my control:

<Canvas Name="canvasTrack" Height="{Binding CanvasHeight}" Width="{Binding CanvasWidth}"> 
   <ItemsControl  
         ItemsSource="{Binding Path=CurrentSegments}" 
         ItemTemplate="{StaticResource BegSegmentTemplate}">
    </ItemsControl>
</Canvas>

And the following DataTemplate:

<DataTemplate x:Key="BegSegmentTemplate">
    <Ellipse Height="10" Width="10" Margin="{Binding EntryPoint}" Fill="Black" HorizontalAlignment="Center" />
</DataTemplate>

I then have it bound to an ObservableCollection that has two items in it. When I run the program two ellipses appear. The first one is in the correct spot on my canvas but the second one is in a 'random' spot. I have checked the numbers that are being bound and everything appears normal. What could I be missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美煞众生 2024-12-20 16:50:19

默认情况下,ItemsControl 循环遍历其项目并将每个项目放入 StackPanel 中,因此您的最终标记基本上如下所示:

<Canvas>
    <StackPanel>
        <ContentPresenter>
            <Ellipse Margin="{Binding EntryPoint}" />
        </ContentPresenter>
        <ContentPresenter>
            <Ellipse Margin="{Binding EntryPoint}" />
        </ContentPresenter>
    </StackPanel>
</Canvas>

如果您想循环遍历项目并放置它们位于基于某些绑定值的 Canvas 上,您需要覆盖 ItemsPanelTemplate 以使用 Canvas 而不是 StackPanel代码>,然后应用您在 ItemContainerStyle 中的定位,以便您在 ContentPresenter 上设置定位,而不是 Ellipse

这将使您的最终结果如下所示:

<Canvas>
    <ContentPresenter Margin="{Binding EntryPoint}">
        <Ellipse />
    </ContentPresenter>
    <ContentPresenter Margin="{Binding EntryPoint}">
        <Ellipse />
    </ContentPresenter>
</Canvas>

实现此目的的一些示例代码如下:

<ItemsControl ItemsSource="{Binding CurrentSegments}">

    <!-- ItemsPanelTemplate -->
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <!-- ItemContainerStyle -->
    <ItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="Margin" Value="{Binding EntryPoint}" />
        </Style>
    </ItemsControl.ItemContainerStyle>

    <!-- ItemTemplate -->
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Ellipse Height="10" Width="10" Fill="Black" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>

</ItemsControl>

请参阅此链接对于一些使用 ItemsControl 的示例

By default, an ItemsControl loops through it's items and puts each of them in a StackPanel, so your end markup basically looks like this:

<Canvas>
    <StackPanel>
        <ContentPresenter>
            <Ellipse Margin="{Binding EntryPoint}" />
        </ContentPresenter>
        <ContentPresenter>
            <Ellipse Margin="{Binding EntryPoint}" />
        </ContentPresenter>
    </StackPanel>
</Canvas>

If you want to loop through items and place them on a Canvas based on some bound value, you need to overwrite the ItemsPanelTemplate to use a Canvas instead of a StackPanel, and apply your positioning in the ItemContainerStyle so that you are setting the positioning on the ContentPresenter, and not the Ellipse

This will make your end result look like:

<Canvas>
    <ContentPresenter Margin="{Binding EntryPoint}">
        <Ellipse />
    </ContentPresenter>
    <ContentPresenter Margin="{Binding EntryPoint}">
        <Ellipse />
    </ContentPresenter>
</Canvas>

Some example code to achieve this would be:

<ItemsControl ItemsSource="{Binding CurrentSegments}">

    <!-- ItemsPanelTemplate -->
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <!-- ItemContainerStyle -->
    <ItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="Margin" Value="{Binding EntryPoint}" />
        </Style>
    </ItemsControl.ItemContainerStyle>

    <!-- ItemTemplate -->
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Ellipse Height="10" Width="10" Fill="Black" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>

</ItemsControl>

See this link for some samples using an ItemsControl

终遇你 2024-12-20 16:50:19

尝试在 ItemsControl 中指定 ItemsPanel

<ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <Canvas />
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>

Try specifying an ItemsPanel in your ItemsControl:

<ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <Canvas />
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文