1

I have one file GraphView.XAML. I have split the Resources section into two ResourceDictionary files (Vertices.xaml and Edges.xaml) which I merge as follows:

GraphView.XAML

<Window x:Class="graph_app.GraphView" ... >

<Grid>
    <Grid.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Design/Vertices.xaml"/>
                <ResourceDictionary Source="Design/Edges.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Grid.Resources>
    ...
<\Grid>

If not split the code works, but if split I get an error in the Vertices.xaml, telling me that the method ChangeVertexColor_OnClick cannot be resolved:

Vertices.XAML

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:graph_app">

      <Style TargetType="{x:Type controls:VertexControl}">
          <EventSetter Event="MouseDoubleClick" Handler="ChangeVertexColor_OnClick"/>
                                                        ^^^cannot resolve symbol^^^^
      </Style>
</ResourceDictionary>

I repeat, if put in one single XAML the code works. Moreover, the ChangeVertexColor_OnClick method is implemented in GrapView.xaml.cs so it should be recognized, but somehow after the split the Vertices.xaml is losing track of the x:Class (I suppose it ignores its existence since it's a separated file).

How can I access ChangeVertexColor_OnClick from the separated ResourcesDictionary file?

Thanks

mitxael
  • 52
  • 7

1 Answers1

1

Nothing strange is happening here :) - it just shouldn't work, because this is the way it's designed. I can see 2 possible solutions:

  1. Add codebehind file for your resource dictionary, and put your ChangeVertexColor_OnClick method there read more here
  2. Do not set MouseDoubleClick in style, but directly on controls:VertexControl (so kind of, revert your split change)
Jonatan Dragon
  • 4,675
  • 3
  • 30
  • 38
  • Thank you @walkerbox , with the solution #1 I'm now able to execute the method OnClick, but since it uses properties of the Current Window (namespace:graph_app) from which the click is received, I was wondering what's the better way to access them from the new class (namespace:graph_app.Design). Thank you! – mitxael Mar 23 '18 at 14:49
  • I would rather not set events in styles and not use code behind for resource dictionaries. if you want to write nice code, read about MVVM pattern. It's perfect choice for WPF/UWP. if you are new, MVVM is bigger topic, so as a workaround, you could: 1. Not set event in ResourceDictionary or 2. Write a helper class to store your data. – Jonatan Dragon Mar 23 '18 at 15:00
  • I've implemented the helper and now all is working like a charm! Thank you @walkerbox – mitxael Mar 23 '18 at 15:40