如何以编程方式更改 WPF 中的 DockPanel?
注意:根据请求,我已添加 XAML 和 xaml.cs 文件的完整代码。
在 WPF 中,我创建了一个 DockPanel
,如下所示:
<Window x:Class="RealEditor.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="RealEditor" Height="500" Width="700">
<DockPanel>
<GridSplitter Grid.Column="1" Width="4" HorizontalAlignment="Left"/>
<DockPanel x:Name="ftpDock" Grid.Row="1" Grid.Column="1"></DockPanel>
</Grid>???
</DockPanel>
</Window>
我想以编程方式将 TreeView
添加到 DockPanel
,但在 Window1.xaml.cs 中,我无法按名称获取 DockPanel
并添加到它:
namespace RealEditor
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
TreeViewItem mytreeView = new TreeViewItem();
ftpDock.Children.Add(myTreeView);
}
}
}
上面的代码返回以下错误:
"The name 'ftpDock' does not exist in the current context"
我确信我刚刚错过了一些简单的事情。有什么想法吗?
Note: By Request, I have added the full code for my XAML and xaml.cs files.
In WPF, I have created a DockPanel
like so:
<Window x:Class="RealEditor.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="RealEditor" Height="500" Width="700">
<DockPanel>
<GridSplitter Grid.Column="1" Width="4" HorizontalAlignment="Left"/>
<DockPanel x:Name="ftpDock" Grid.Row="1" Grid.Column="1"></DockPanel>
</Grid>???
</DockPanel>
</Window>
I want to programmatically add a TreeView
to the DockPanel
, but in Window1.xaml.cs, I am unable to get the DockPanel
by name, and add to it:
namespace RealEditor
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
TreeViewItem mytreeView = new TreeViewItem();
ftpDock.Children.Add(myTreeView);
}
}
}
The above code returns the following error:
"The name 'ftpDock' does not exist in the current context"
I am sure I have just missed something simple. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,您的 XAML 已损坏。您有一个额外的
,它与打开的 Grid 标记无关。
我刚刚创建了一个项目,复制了您的 XAML 和代码,除了大写字母差异和修复额外标签之外,我的工作正常。
我的完整 XAML:
代码隐藏:
检查所有标签并确保没有任何随机的无关标签浮动
Firstly, your XAML is broken. You have an extra
</Grid>
which is un-associated with opening Grid tag.I just created a project, copied your XAML and Code behind in, and apart from the captilisation difference and fixing the extra tag, mine works fine.
My Full XAML:
Code Behind:
Check all your tags and make sure you don't have any random extraneous tags floating around
在您的 xaml 中,您是否具有窗口的正确类属性?
请参阅此处:WPF C# 类、文本框和引用,Easy(?)“当前上下文中不存在”
In your xaml, do you have the right class attribute for the window?
See here: WPF C# Classes, TextBox and Reference, Easy(?) "does not exist in the current context"
经过进一步的测试和故障排除,我发现我无法引用添加到 Window1.xaml 中的任何控件,而不仅仅是 DockPanel。要解决此问题,我必须将“MSBuild:Compile”添加到 Window1.xaml 文件属性中的“自定义构建”字段。
我不确定为什么该字段在我的项目中是空白的,但希望这能帮助其他遇到相同“错误”的人。
Upon further testing and troubleshooting, I found that I was unable to reference any control added to Window1.xaml, not just the DockPanel. To fix the issue, I had to add "MSBuild:Compile" to the Custom Build field in the Window1.xaml File Properties.
I am unsure why that field was blank in my project, but hopefully this will help others experiencing the same "bug".