WPF StackPanel 失去了它的子项

发布于 2024-10-27 00:50:41 字数 939 浏览 8 评论 0原文

我试图解决以下问题:

我有一个包含图像的 StackPanel。但如果我执行 panel.Children.Count 它有时会返回 0,有时会返回 1。为什么?我该如何解决这个问题? 注意:我从 MouseEvent 的发送者对象获取 StackPanel。

有谁知道如何解决我的问题?感谢任何帮助:)

更新:

private void MoveContainer(object sender, MouseEventArgs e)
{
    StackPanel panel = sender as StackPanel;

    MessageBox.Show(panel.Children.Count.ToString());
}

更新 2:

<StackPanel Name="a1" AllowDrop="True"
                    PreviewMouseLeftButtonDown="StartDrag"
                    PreviewMouseMove="MoveContainer"
                    Visibility="Visible"
                    Background="Transparent">
            <Image Source="testcontainer.png" Width="60" Height="30"></Image>
</StackPanel>

更新 3:

private void StartDrag(object sender, MouseEventArgs e)
{

    startPos=e.GetPosition(null); //startPos is a local attribute
}

im trying to solve the following problem:

I have a StackPanel that contains an Image as. But if I do panel.Children.Count it sometimes returns 0 and sometimes 1. Why? How can I fix this?
Note: I get the StackPanel from the sender Object of an MouseEvent.

Does anyone has an idea how to solve my problem? Any help is appreciated:)

Update:

private void MoveContainer(object sender, MouseEventArgs e)
{
    StackPanel panel = sender as StackPanel;

    MessageBox.Show(panel.Children.Count.ToString());
}

Update 2:

<StackPanel Name="a1" AllowDrop="True"
                    PreviewMouseLeftButtonDown="StartDrag"
                    PreviewMouseMove="MoveContainer"
                    Visibility="Visible"
                    Background="Transparent">
            <Image Source="testcontainer.png" Width="60" Height="30"></Image>
</StackPanel>

Update 3:

private void StartDrag(object sender, MouseEventArgs e)
{

    startPos=e.GetPosition(null); //startPos is a local attribute
}

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

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

发布评论

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

评论(1

葬花如无物 2024-11-03 00:50:41

我测试了代码,它总是为我返回 1,但是信息太少,无法知道您的问题出在哪里。

你修改了StackPanel的多少个地方?
该事件处理程序是否被其他没有子项的 StackPanel 使用?
您是否使用了一些奇怪的 ControlTemplates 来导致嵌套的 StackPanels?

I tested the code and it always returns 1 for me, there is way too few information to know what is wrong on your end though.

In how many places do you modify the StackPanel?
Is that event handler used by other StackPanels which have no children?
Do you use some weird ControlTemplates which cause nested StackPanels?

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