-2

I am currently trying to add functionality to a ResourceDictionary, by declaring it's x:Class and linking an On Click event to a function within said class. Here is an example:

Where I link the x:Class enter image description here

Where I link an x:Class function to an On Click event: enter image description here

And the source for my x:Class:

using System;
using System.Windows;

namespace VoidwalkerEngine.Editor.Resources.Themes.Styles
{
    public partial class VoidwalkerCellBrowserTreeView : ResourceDictionary
    {
        public VoidwalkerCellBrowserTreeView()
        {
            InitializeComponent();
        }

        private void BaseTreeView_NewFolder_Click(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("Test"); // This should be fired when I click on "New Folder"
        }
    }
}

A picture of the menu item just before I click it: enter image description here

After I click the menu item, it should print "Test" to the console. However, nothing happens. Clearly, I must be doing something wrong. I also found a similar question to mine located here: Control event not firing from class linked to resource dictionary wpf

And their suggestion was to add an extra line to the .csproj file, which I did: enter image description here

However, this still is not working. Obviously I'm still not linking something correctly, I'm just at a loss of how to proceed from here. Does anyone know how to properly link a ResourceDictionary with it's x:Class? My project is throwing no errors, and Visual Studio even auto-completed the BaseTreeView_NewFolder_Click function into the x:Class file, so I know the source file itself is attached just fine.

EDIT 1: Here is the full XAML ResourceDictionary: https://pastebin.com/8UepKGTa

EDIT 2: After testing a few things, I noticed something very peculiar. Apparently, any console command I place in the default constructor will be fired, but no methods will be fired. Here is an image:

enter image description here I'm seriously at a loss right now. The class IS linking just fine, but for whatever reason, the functions won't link to it.

Krythic
  • 4,184
  • 5
  • 26
  • 67
  • Yeah, I've tried everything. I have no idea why this is not working. I'm going to open this up for bounty after the required elapsed time. – Krythic Apr 21 '18 at 21:31
  • Reason for the downvote? Is there a way that I can improve this question? – Krythic Apr 21 '18 at 22:48
  • What if you put a call to `MessageBox.Show("")` in the event handler? – mm8 Apr 23 '18 at 12:20
  • @mm8 Thank you for your response. Your question results in the same as mentioned above, the only code that can be executed is within the constructor of said class. No functions are fired. I still don't know why. – Krythic Apr 23 '18 at 18:15

1 Answers1

1

You don't need to manually add that stuff in csprroj. It works rather like a window. You need the class to be referred to in the resource dictionary:

The code file must inherit from resourcedictionary and be a partial class:

namespace MapEditor
{
    public partial class TerrainResources : ResourceDictionary

And you need the initializecomponent stuff:

    public TerrainResources()
    {
        InitializeComponent();
    }

The class properties build action should be compile compile And of course the resource dictionary must be build action page.

Looking at your code, at first glance it looks like it ought to work. You have quite a long namespace. VS doesn't get on well with deep folder structures and very long namespaces.

Where is your menuitem? I'm using my code there for a loaded event, and that's used from datatemplates inside that resource dictionary:

<DataTemplate DataType="{x:Type local:SwampVM}">
    <Polygon Points="{Binding Points}"
             Fill="YellowGreen"
             local:TerrainProp.TerrainCanvas="{Binding RelativeSource={RelativeSource AncestorType={x:Type Canvas}}}"
             FrameworkElement.Loaded="Terrain_Loaded"

Your contextmenu would have to be a resource in the resource dictionary to work with that event.

Andy
  • 11,864
  • 2
  • 17
  • 20
  • The class is set to "Compile" and the xaml is set to "Page" already. Give me a few minutes, because I'm trying a few other things right now. I really hate moments like this, because everything looks like it should be working. – Krythic Apr 21 '18 at 20:12
  • Well, one thing I just noticed was that I had misspelled DependentUpon, (Why did Visual Studio not alert me?), and even after fixing that, it still doesn't work. – Krythic Apr 21 '18 at 20:19
  • Here is the full XAML Resource Dictionary: https://pastebin.com/8UepKGTa – Krythic Apr 21 '18 at 20:25