诸位老师好!
  我遇到一个问题:下面我叙述一下,请诸位老师看看。
  
  代码中动态加载xaml文件
 
  XmlTextReader xmlreader = new XmlTextReader(xamlFile);//xamlFile为xaml文件的全路径          
  object obj = XamlReader.Load(xmlreader);
  StackPanel root= obj as StackPanel
  this.mainPanel.Children.Add(root);//mainPanel 是个主窗体的一个StackPanel
  这一步没有问题。
xaml文件如下   <StackPanel Name="root" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:my="clr-namespace:DevComponents.WpfEditors;assembly=DevComponents.WpfEditors" 
          xmlns:xctk="clr-namespace:Xceed.Wpf.Toolkit;assembly=WPFToolkit.Extended"        
                   Height="581" Width="956" Background="Blue">
   <Grid Name="grid">
                <Label Content="中文名称" Height="28" HorizontalAlignment="Left" Margin="12,28,0,0" Name="label1" VerticalAlignment="Top"  />
                <TextBox Height="23" HorizontalAlignment="Left" Margin="94,28,0,0" Name="textBox_chinesename" VerticalAlignment="Top" Width="209" />
                <TextBlock Height="23" HorizontalAlignment="Left" Margin="309,28,0,0" Name="textBlock_chinesename"  Foreground="Red" VerticalAlignment="Top" Width="152" />
                
                
                <Label Content="语言" Height="28" HorizontalAlignment="Left" Margin="478,28,0,0" Name="label3" VerticalAlignment="Top" />
                <ComboBox Height="23" HorizontalAlignment="Left" Margin="560,33,0,0" Name="comboBox_language" SelectedIndex="1" VerticalAlignment="Top" Width="209" >
                    <ComboBoxItem>中文</ComboBoxItem>
                    <ComboBoxItem>英文</ComboBoxItem>
                </ComboBox>
       </Grid>
       
       <StackPanel Name="stackPanel">
              <Label Content="推荐指数" Height="28" HorizontalAlignment="Left" Margin="12,135,0,0" Name="label11" VerticalAlignment="Top" Width="74" />
                <my:DoubleInput HorizontalAlignment="Left" Margin="94,141,0,0" Name="doubleInput_recommend" VerticalAlignment="Top" Width="209" MinValue="0" Value="9" MaxValue="10" NavigationKeyValueChange="True" />
                <TextBlock Foreground="Red" Height="23" HorizontalAlignment="Right" Margin="0,141,495,0" Name="textBlock_recommend" VerticalAlignment="Top" Width="152" />                <Label Content="介绍" HorizontalAlignment="Left" Margin="478,172,0,381" Name="label16" Width="74" />
                <TextBox Height="172" HorizontalAlignment="Left" Margin="562,107,0,0" Name="textBox_description" VerticalAlignment="Top" Width="207" MinLines="5" />
                <TextBlock Foreground="Red" Height="23" HorizontalAlignment="Right" Margin="0,257,18,0" Name="textBlock_description" VerticalAlignment="Top" Width="165" />                <Label Content="排行" Height="28" HorizontalAlignment="Left" Margin="12,192,0,0" Name="label4" VerticalAlignment="Top" />
                <my:IntegerInput HorizontalAlignment="Left" Margin="91,198,0,0" MinValue="0" Name="integerInput_rank" Value="100" VerticalAlignment="Top" Width="212" />
                <TextBlock Foreground="Red" Height="23" HorizontalAlignment="Left" Margin="309,197,0,0" Name="textBlock_rank" VerticalAlignment="Top" Width="152" />
       
       </StackPanel>
 </StackPanel>
我的问题:动态加载了,之后,root.FindName。是正常的。Grid grid = root.FindName("grid");//没有问题。
grid.FindName("控件名称") //就为null 
grid.Children 是没有问题的。通过grid.Children[index]没有问题。
请问诸位老师这是为什么呢?也就是说,动态加载的xaml 文件,如果布局管理器,不是xaml的根元素,则调用该布局管理器的FindName就为null多谢了。

解决方案 »

  1.   

    XamlReader中的元素 需要加 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"才能被找到
      

  2.   

    <StackPanel Name="root" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"                
                                 xmlns:my="clr-namespace:DevComponents.WpfEditors;assembly=DevComponents.WpfEditors" 
                                 xmlns:xctk="clr-namespace:Xceed.Wpf.Toolkit;assembly=WPFToolkit.Extended"                         
                       Height="581" Width="956" Background="Blue">
              <Grid Name="grid"   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        
              xmlns:my="clr-namespace:DevComponents.WpfEditors;assembly=DevComponents.WpfEditors"  
              xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"          
             Name="grid"  HorizontalAlignment="Center" VerticalAlignment="Stretch" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto">
                    <Label Content="中文名称" Height="28" HorizontalAlignment="Left" Margin="12,28,0,0" Name="label1" VerticalAlignment="Top"  />
                    <TextBox Height="23" HorizontalAlignment="Left" Margin="94,28,0,0" Name="textBox_chinesename" VerticalAlignment="Top" Width="209" />
                    <TextBlock Height="23" HorizontalAlignment="Left" Margin="309,28,0,0" Name="textBlock_chinesename"  Foreground="Red" VerticalAlignment="Top" Width="152" />
                     
                     
                    <Label Content="语言" Height="28" HorizontalAlignment="Left" Margin="478,28,0,0" Name="label3" VerticalAlignment="Top" />
                    <ComboBox Height="23" HorizontalAlignment="Left" Margin="560,33,0,0" Name="comboBox_language" SelectedIndex="1" VerticalAlignment="Top" Width="209" >
                        <ComboBoxItem>中文</ComboBoxItem>
                        <ComboBoxItem>英文</ComboBoxItem>
                    </ComboBox>
           </Grid>
            
           <StackPanel Name="stackPanel">
                       <Label Content="推荐指数" Height="28" HorizontalAlignment="Left" Margin="12,135,0,0" Name="label11" VerticalAlignment="Top" Width="74" />
                    <my:DoubleInput HorizontalAlignment="Left" Margin="94,141,0,0" Name="doubleInput_recommend" VerticalAlignment="Top" Width="209" MinValue="0" Value="9" MaxValue="10" NavigationKeyValueChange="True" />
                    <TextBlock Foreground="Red" Height="23" HorizontalAlignment="Right" Margin="0,141,495,0" Name="textBlock_recommend" VerticalAlignment="Top" Width="152" />
     
                    <Label Content="介绍" HorizontalAlignment="Left" Margin="478,172,0,381" Name="label16" Width="74" />
                    <TextBox Height="172" HorizontalAlignment="Left" Margin="562,107,0,0" Name="textBox_description" VerticalAlignment="Top" Width="207" MinLines="5" />
                    <TextBlock Foreground="Red" Height="23" HorizontalAlignment="Right" Margin="0,257,18,0" Name="textBlock_description" VerticalAlignment="Top" Width="165" />
     
                    <Label Content="排行" Height="28" HorizontalAlignment="Left" Margin="12,192,0,0" Name="label4" VerticalAlignment="Top" />
                    <my:IntegerInput HorizontalAlignment="Left" Margin="91,198,0,0" MinValue="0" Name="integerInput_rank" Value="100" VerticalAlignment="Top" Width="212" />
                    <TextBlock Foreground="Red" Height="23" HorizontalAlignment="Left" Margin="309,197,0,0" Name="textBlock_rank" VerticalAlignment="Top" Width="152" />
              
           </StackPanel>
     </StackPanel>然后
    Grid grid  root.FindName("grid") as Grid;
    grid.FindName("控件名称");//还是null
    是这样的吗?还是我错误的理解了您的意思?
      

  3.   

    不知道是不是我回答的不对 
    但是 findname我以前也遇到过找不到的问题
    主要是你没有加入注册RegisterName("newButton", btn);类似这样
    http://www.cnblogs.com/wj-love/archive/2012/09/12/2681717.html 这篇文章也可以看看
    我前面说的没有加xmlns是指xaml的load时候不加好像会报个错什么的 和findname好像关系不大
      

  4.   

     http://www.cnblogs.com/wj-love/archive/2012/09/12/2681717.html 
    妹妹的 居然把链接粘在一起了 报错 呵呵 你单独点开看下吧 希望对你有帮助