CathalMF
CathalMF

Reputation: 10055

Attach to a click event in XAML control when loading XAML dynamically.

I have a XAML control which gets loaded dynamically at runtime. This is pure XAML with no code behind.

I dont have any control over the parent loading mechanism which is why this looks a bit weird.

I have a parent application which loads my Plugin DLL and loads my XAML Control.

My DLL signature is:

public class Application : BaseClassHere
{
    public Application(IParentContext context) : base(context)
    {
        // Im placing this instance in the bag which i use in my XAML
        base.MyObservablePropertyBag["MyParentContext"] = new ObservableValue<object>(this);
    }
}

My XAML is like this:

<av:UserControl 
    xmlns:local="clr-namespace:MyApplicationNS;assembly=MyDll"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:av="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" >

    <Border DataContext="{Binding MyObservablePropertyBag[MyParentContext].Value}" >

        <Button Name="MyButton" />

    </Border>
</UserControl>

This binding works perfectly in the XAML. I have full access to all public properties that i definein the Application class.

My problem is that i want to link to up Click events on my Buttons. If i add a Click event in the XAML it errors at runtime telling me that i need to compile my XAML.

Is there any way to subscribe to the Click event on MyButton in the Application class?

Upvotes: 0

Views: 325

Answers (1)

Markus H&#252;tter
Markus H&#252;tter

Reputation: 7906

Well, if you say binding works then why try to mess with the Click event? Rather go with the Button's Command property. Create an ICommand instance (like a DelegateCommand or RelayCommand) that you place in your propertybag and just bind to it!

Upvotes: 1

Related Questions