日期:2014-05-17  浏览次数:21176 次

WPF,请看下这个奇怪现象
一个UserControl:

<UserControl x:Class="WPF1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="200" d:DesignWidth="200" Background="Blue">
    <Grid>
       <Button Content="张三" FontSize="30" Height="100" Width="100" Background="Red" Click="button1_Click"/>
    </Grid>
</UserControl>



public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        GetWeiShu(this);
    }
    public void GetWeiShu(DependencyObject obj)
    {
        Button button = null;
        DependencyObject d;
        foreach (var c in LogicalTreeHelper.GetChildren(obj))
        {
            d = c as DependencyObject;
            if (d != null)
            {
                button = d as Button;
                if (button != null)
                {
                    MessageBox.Show("ss");
                    break;
                }
                GetWeiShu(d);//递归执行。
            }
        }
        MessageBox.Show("李四");
    }
}



<Window x:Class="WPF1.MainWindow"
        xmlns="http://schemas.microsoft.com/w