如何以编程方式更改 WPF 中的 DockPanel?

发布于 2024-08-18 06:20:31 字数 1311 浏览 7 评论 0原文

注意:根据请求,我已添加 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

囚你心 2024-08-25 06:20:31

首先,您的 XAML 已损坏。您有一个额外的 ,它与打开的 Grid 标记无关。

我刚刚创建了一个项目,复制了您的 XAML 和代码,除了大写字母差异和修复额外标签之外,我的工作正常。

我的完整 XAML:

<Window x:Class="WPFTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <DockPanel>
        <GridSplitter Grid.Column="1" Width="4" HorizontalAlignment="Left"/>
        <DockPanel x:Name="ftpDock" Grid.Row="1" Grid.Column="1"></DockPanel>
    </DockPanel>
</Window>

代码隐藏:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        TreeViewItem mytreeView = new TreeViewItem();
        ftpDock.Children.Add(mytreeView);
    }
}

检查所有标签并确保没有任何随机的无关标签浮动

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:

<Window x:Class="WPFTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <DockPanel>
        <GridSplitter Grid.Column="1" Width="4" HorizontalAlignment="Left"/>
        <DockPanel x:Name="ftpDock" Grid.Row="1" Grid.Column="1"></DockPanel>
    </DockPanel>
</Window>

Code Behind:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        TreeViewItem mytreeView = new TreeViewItem();
        ftpDock.Children.Add(mytreeView);
    }
}

Check all your tags and make sure you don't have any random extraneous tags floating around

握住我的手 2024-08-25 06:20:31

在您的 xaml 中,您是否具有窗口的正确类属性?

<Window x:Class="YourAssemblyName.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
            <DockPanel Grid.Row="1" x:Name="ftpDock" Grid.Column="1"></DockPanel>
    </Grid>
</Window>

请参阅此处:WPF C# 类、文本框和引用,Easy(?)“当前上下文中不存在”

In your xaml, do you have the right class attribute for the window?

<Window x:Class="YourAssemblyName.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
            <DockPanel Grid.Row="1" x:Name="ftpDock" Grid.Column="1"></DockPanel>
    </Grid>
</Window>

See here: WPF C# Classes, TextBox and Reference, Easy(?) "does not exist in the current context"

瞄了个咪的 2024-08-25 06:20:31

经过进一步的测试和故障排除,我发现我无法引用添加到 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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文