开发者

WPF StackPanel has lost its child

开发者 https://www.devze.com 2023-02-20 03:35 出处:网络
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?

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="testco开发者_StackOverflow中文版ntainer.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
}


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消