注意:根据请求,我已为我的 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>
我想以编程方式向 Window1.xaml.cs 添加一个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"
我确定我只是错过了一些简单的事情。有任何想法吗?