0

I have a behavior for Window as follows

<Window>
    <my:Notify x:Name="Not"/>
    <behaviors:Interaction.Behaviors>
        <behavior:RebuildBehavior Element="{Binding ElementName=Not}" />
    </behaviors:Interaction.Behaviors>
<Window>

now i want to write this code in code behind, so i used this code:

in Notify.cs (Loaded Event):

RebuildBehavior behavior = new RebuildBehavior();
behavior.Element = this;
Interaction.GetBehaviors(this).Add(behavior);

But my app crashes in the last line Interaction.GetBehaviors(this).Add(behavior);

System.Resources.MissingManifestResourceException: 'Could not find the resource "ExceptionStringTable.resources" among the resources "

Did I write the correct code?

UPDATE: I moved codes to window.cs (Loaded event)

RebuildBehavior behavior = new RebuildBehavior();
behavior.Element = Notify.Instance;
Interaction.GetBehaviors(this).Add(behavior);

crash fixed but not working

karma
  • 147
  • 7
  • I can't reproduce the issue. Similar code does work by my. It must be something in `RebuildBehavior` which you didn't post. – Rekshino Nov 12 '21 at 07:20
  • Have you checked all cases from [What does MissingManifestResourceException mean and how to fix it?](https://stackoverflow.com/questions/1327692/what-does-missingmanifestresourceexception-mean-and-how-to-fix-it) – Rekshino Nov 12 '21 at 07:25
  • @Rekshino i moved codes to window.cs and now crash fixed but, expected behavior not work – karma Nov 12 '21 at 07:34
  • Try to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) and post new question. – Rekshino Nov 12 '21 at 07:40

1 Answers1

0

The following code would be the equivalent of your XAML markup:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        RebuildBehavior behavior = new RebuildBehavior();
        BindingOperations.SetBinding(behavior, RebuildBehavior.ElementProperty, 
            new Binding() { ElementName ="Not" });
        Interaction.GetBehaviors(this).Add(behavior);
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88