views:

432

answers:

3

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>

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?

A: 

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: http://stackoverflow.com/questions/1503749/wpf-c-classes-textbox-and-reference-easy-does-not-exist-in-the-current-con

Eric Dahlvang
Yes, the class attribute is correct in XAML, and I am attempting to access the DockPanel in the Window1 : Window class.
CheapDevotion
Where in the Window1 class? And what exactly does your xaml look like? Cuz a .Window1 file like what I've posted above will allow you to access the ftpDock in the codebehind. It is impossible to know how to fix your problem without more information.
Eric Dahlvang
Added full code to original post.
CheapDevotion
+1  A: 

Firstly, your XAML is broken. You have an extra 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

Alastair Pitts
A: 

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".

CheapDevotion
how strange, good to hear you got it solved.
Alastair Pitts