So, I created a child class based off a base XAML (see below), but I created the child class in another project and Visual Studio gives me an error on the TreeView saying "The name "ViewTree" does not exist in the current context". However, the other child classes that exist, which are located inside the same project do not have the same error. Why is this?
I've added a reference to the other project, and used the same namespace, but still no joy.
Thanks!
Base XAML:
<UserControl x:Class="myLibrary.Material.ViewTreeSpace.ViewingTree"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Background="#00000000" >
<DockPanel>
<TreeView Name="ViewTree" />
</DockPanel>
</UserControl>
Parent code-behind:
namespace myLibrary.Material.ViewTreeSpace {
public partial class ViewingTree : Usercontrol {
public string TreeName = String.Empty;
public ViewingTree(){ }
}
Child class in different project:
namespace myLibrary.Material.ViewTreeSpace {
public class ImportedTree : ViewingTree {
public ImportedTree() : base() {
ViewTree.Items.Clear(); // <- This line gives me error
TreeName = "My imported Tree in another project."; // <- This works
}
}
Edit: I've also noticed that variables that exist in code-behind can be referenced, only UIElements from the cannot.
Edit: Upon receiving @Liju answer, I set my TreeView to use the x:FieldModifier="public"
. However, I get the below error now.
System.Exception: The component 'myLibrary.Material.ViewTreeSpace.ImportedTree' does not have a resource identified by the URI '/myLibrary;component/material/view%20tree/viewingtree.xaml'.
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at myLibrary.Material.ViewTreeSpace.ViewingTree.InitializeComponent()
at myLibrary.Material.ViewTreeSpace.ViewingTree..ctor()
at myLibrary.Material.ViewTreeSpace.ImportedTree..ctor() in c:\..\ImportedTree.cs:line 51
From what I could find, it isn't possible to inherit a XAML class from a different assembly. (i.e. Answer for "The component does not have a resource identified by the URI Apparently, this has been a problem since 2008.
Edit 2:
Using @Liju's second answer, I tried setting the ResourceDictionary, but it says it fails to load the resource.
System.InvalidOperationException: ResourceDictionary LoadFrom operation failed with URI 'pack://application:,,,/myLibrary;component/Material/View Tree/ViewingTree.xaml'.
at System.Windows.ResourceDictionary.set_Source(Uri value)
at myLibrary.Material.ViewTreeSpace.ImportedTree..ctor() in c:\..\ImportedTree.cs:line 59