【问题标题】:How do I programatically change a DockPanel in WPF?如何以编程方式更改 WPF 中的 DockPanel?
【发布时间】:2011-01-05 21:30:03
【问题描述】:

注意:应要求,我已为我的 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"

我确定我错过了一些简单的事情。有什么想法吗?

【问题讨论】:

  • 显示更多代码、XAML 和后面的代码。

标签: c# wpf wpf-controls dockpanel


【解决方案1】:

首先,您的 XAML 已损坏。您有一个额外的 &lt;/Grid&gt; 与打开的 Grid 标记无关。

我刚刚创建了一个项目,将您的 XAML 和代码复制到其中,除了 captilisation 差异和修复额外标签之外,我的工作正常。

我的完整 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);
    }
}

检查你所有的标签,确保你没有任何随机的无关标签漂浮在周围

【讨论】:

    【解决方案2】:

    在你的 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# Classes, TextBox and Reference, Easy(?) "does not exist in the current context"

    【讨论】:

    • 是的,类属性在 XAML 中是正确的,我正在尝试访问 Window1 中的 DockPanel:Window 类。
    • Window1 类在哪里?你的 xaml 到底是什么样的?因为像我在上面发布的那样的 .Window1 文件将允许您访问代码隐藏中的 ftpDock。如果没有更多信息,就不可能知道如何解决您的问题。
    【解决方案3】:

    在进一步测试和故障排除后,我发现我无法引用添加到 Window1.xaml 的任何控件,而不仅仅是 DockPanel。要解决此问题,我必须将“MSBuild:Compile”添加到 Window1.xaml 文件属性中的自定义构建字段。

    我不确定为什么我的项目中该字段为空白,但希望这将有助于其他遇到相同“错误”的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-01
      • 1970-01-01
      • 2012-12-06
      • 1970-01-01
      相关资源
      最近更新 更多