如何从代码隐藏中访问 ItemPanelTemplate 中的命名控件?

发布于 2024-10-18 04:08:05 字数 950 浏览 1 评论 0原文

我正在尝试访问 ItemsPanelTemplate 中的 Canvas 对象,以便我可以在代码隐藏中直接向其添加项目。

这是我的 XAML:

 <ListBox x:Name="MyMap" ItemsSource="{Binding MapItems}" 
        Background="Gray"
        SelectionChanged="MyMap_SelectionChanged">

    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas x:Name="MyMapCanvas"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Background="Transparent">
            </Canvas>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>

如何访问代码隐藏中的“MyMapCanvas”以向其添加项目?

我已经尝试过以下方法:

Canvas canvas = (Canvas)MyMap.ItemsPanel.LoadContent();

ContentPresenter cp = (ContentPresenter)(VisualTreeHelper.GetChild(MyMap, 0));
ItemsPanelTemplate ipt = MyMap.ItemsPanel;
Canvas canvas = ipt.FindName("MyMapCanvas", cp) as Canvas;

提前致谢。

I am trying to access a Canvas object within an ItemsPanelTemplate so I can add items directly to it in the code-behind.

Here is my XAML:

 <ListBox x:Name="MyMap" ItemsSource="{Binding MapItems}" 
        Background="Gray"
        SelectionChanged="MyMap_SelectionChanged">

    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas x:Name="MyMapCanvas"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Background="Transparent">
            </Canvas>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>

How can I access "MyMapCanvas" in the code-behind to add items to it?

I have tried the following:

Canvas canvas = (Canvas)MyMap.ItemsPanel.LoadContent();

and

ContentPresenter cp = (ContentPresenter)(VisualTreeHelper.GetChild(MyMap, 0));
ItemsPanelTemplate ipt = MyMap.ItemsPanel;
Canvas canvas = ipt.FindName("MyMapCanvas", cp) as Canvas;

Thanks in advance.

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

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

发布评论

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

评论(1

愚人国度 2024-10-25 04:08:05

试试这个:

    //your OnLoaded handler
    private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
    {
        Canvas canvas = FindVisualChild<Canvas>(MyMap);
    }

    public TChildItem FindVisualChild<TChildItem>(DependencyObject obj) where TChildItem : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            var child = VisualTreeHelper.GetChild(obj, i);

            if (child != null && child is TChildItem)
                return (TChildItem)child;

            var childOfChild = FindVisualChild<TChildItem>(child);

            if (childOfChild != null)
                return childOfChild;
        }

        return null;
    }

Try this:

    //your OnLoaded handler
    private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
    {
        Canvas canvas = FindVisualChild<Canvas>(MyMap);
    }

    public TChildItem FindVisualChild<TChildItem>(DependencyObject obj) where TChildItem : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            var child = VisualTreeHelper.GetChild(obj, i);

            if (child != null && child is TChildItem)
                return (TChildItem)child;

            var childOfChild = FindVisualChild<TChildItem>(child);

            if (childOfChild != null)
                return childOfChild;
        }

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