Apologies for the length of this question, but it is rather intricate.
There is a lot of code around this, so I am only showing the code related to the problem.
I am using Loaded as the event, but the problem is the same regardless of which event I try.
I have a custom-control that defines a public FrameworkElement property.
When the custom-control is used, you can set this property on the control to a Grid, for example, and populate that with whatever you want.
Within the custom-control itself, the property is bound as the Content of a ContentPresenter.
This works fine.
This is being used to present a number of different objects, using a common look.
The problem comes when I try to use any of the interaction features inside controls in the CustomControlContent propery.
This is the property defined in the code-behind of the custom-control.
public class CustomControlClass : Control
{
...
public FrameworkElement CustomControlContent
{
get => (FrameworkElement)GetValue(CustomControlContentProperty);
set => SetValue(CustomControlContentProperty, value);
}
public static readonly DependencyProperty CustomControlContentProperty =
DependencyProperty.Register(nameof(CustomControlContent),
typeof(FrameworkElement), typeof(CustomControlClass)
, new PropertyMetadata(null));
...
}
Within the custom-control XAML, this property is bound like this
...
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
...
<Grid IsSharedSizeScope="True">
...
!!! This EvenTrigger in the custom-control itself, works. !!!
<b:Interaction.Triggers>
<b:EventTrigger EventName="Loaded" >
<b:CallMethodAction MethodName="Loaded" TargetObject="{Binding}" />
</b:EventTrigger>
</b:Interaction.Triggers>
...
<Border Grid.Row="1">
<ContentPresenter Content="{TemplateBinding CustomControlContent}" />
</Border>
</Grid>
...
In the TabItem where this custom control is used, the XAML looks like this. I had added a couple of CallMethodActions to demonstrate where they work and where they do not. It is as if the interactions XAML is simply being ignored. For example, if I use a method name that does not exist, I get a missing method exception (naturally) where it is working, but nothing at all where it is not. I have tried an InvokeComamandAction and a Behavior which are also ignored, so it is not just the CallMethodAction that is the problem.
...
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
...
<controls:CustomControlClass>
!!! This works !!!
<b:Interaction.Triggers>
<b:EventTrigger EventName="Loaded" >
<b:CallMethodAction MethodName="CustomControlClassLoaded" TargetObject="{Binding}" />
</b:EventTrigger>
</b:Interaction.Triggers>
<controls:CustomControlClass.CustomControlContent>
!!! When I put this in the XAML, intellisense tells me "A properyy element cannot be the direct content of another property element" !!!
<b:Interaction.Triggers>
<b:EventTrigger EventName="Loaded" >
<b:CallMethodAction MethodName="CustomControlLoaded" TargetObject="{Binding}" />
</b:EventTrigger>
</b:Interaction.Triggers>
<Grid>
!!! This is apparently completely ignored - no intellisense complaints and the containing dialog is shown without problems, even though GridLoaded does not exist
!!!
<b:Interaction.Triggers>
<b:EventTrigger EventName="Loaded" >
<b:CallMethodAction MethodName="GridLoaded" TargetObject="{Binding}" />
</b:EventTrigger>
</b:Interaction.Triggers>
...
Can anyone suggest how I can get these triggers/Behaviour to work?
I have tried adding the b names-space in the Grid itself, but that makes no difference.
David Richards is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1