诸位老师好!
我遇到一个问题:下面我叙述一下,请诸位老师看看。
代码中动态加载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多谢了。
我遇到一个问题:下面我叙述一下,请诸位老师看看。
代码中动态加载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多谢了。
解决方案 »
- 删除回收站下面的所有文件,出现不明白的问题。大牛请来赐教!!
- 急!如何修改Webclient.header属性的Date值啊,使用add方法和AddWithoutValidate 出现"此标头必须使用适当的属性进行修改"错误
- 菜鸟高分请教source safe的使用方法!
- 哪个哥哥知道如何调用C写的DLL
- 菜鸟求教C#的常识问题
- 高手请进,接口问题?
- C# 关于datatable 问题
- 关于socket代理
- 一个关于客户端和服务端通信的问题
- 如何在c#中提供帮助文件?
- 未能加载文件或程序集“PDFLibNet.dll”或它的某一个依赖项。找不到指定的模块。
- 如何获取 EF(.net 3.5)中正在等待处理的项集合?
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
是这样的吗?还是我错误的理解了您的意思?
但是 findname我以前也遇到过找不到的问题
主要是你没有加入注册RegisterName("newButton", btn);类似这样
http://www.cnblogs.com/wj-love/archive/2012/09/12/2681717.html 这篇文章也可以看看
我前面说的没有加xmlns是指xaml的load时候不加好像会报个错什么的 和findname好像关系不大
妹妹的 居然把链接粘在一起了 报错 呵呵 你单独点开看下吧 希望对你有帮助