0

I have a user control defined in my project which contains a control to which I need a reference in my view model. To that end I am handling the loaded event of the user control in my xaml like so:

<UserControl
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:c1="http://schemas.componentone.com/winfx/2006/xaml" 
         xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
         x:Class="SchedulerView" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<i:Interaction.Triggers>
    <i:EventTrigger EventName="Unloaded">
        <i:InvokeCommandAction Command="{Binding Path=SchedulerUnloadedCommand}" />
    </i:EventTrigger>
    <i:EventTrigger EventName="Loaded">
        <i:InvokeCommandAction Command="{Binding Path=SchedulerLoadedCommand}" CommandParameter="{Binding ElementName=scheduler}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

I have used exactly the same sort of CommandParameter binding in other parts of the project and it has worked well, but for some reason the scheduler object to which I require the reference is not getting passed to the command shown below.

Private _schedulerLoadedCommand As ICommand

Public ReadOnly Property SchedulerLoadedCommand As ICommand
    Get
        If _schedulerLoadedCommand Is Nothing Then
            Dim MySchedulerLoaded As New Action(Of Object)(AddressOf SchedulerLoaded)
            _schedulerLoadedCommand = New RelayCommand(MySchedulerLoaded)
        End If
        Return _schedulerLoadedCommand
    End Get
End Property

Private Sub SchedulerLoaded(ByVal obj As Object)
    mrvm.scheduler = DirectCast(obj,C1Scheduler)

End Sub

A breakpoint placed in the SchedulerLoaded sub shows that the value of the parameter obj is Nothing.

Is it possible that there simply is a tangible version of the scheduler control that I can reference at the time of the Loaded event or is the use of elementName incorrect here.

I'd welcome any thoughts that you might have.

Dom Sinclair
  • 2,458
  • 1
  • 30
  • 47
  • 3
    Are you seeing any binding errors in the Output window? Are you sure that your "scheduler" element's DataContext is the object you want? I might be wrong, but I think that the element you're pointing to might not be databound at the time the command is called (because you're using the UserControl's Loaded event). – vesan Apr 01 '15 at 22:26
  • @vesan Thank you for your thoughts. The output window is clear of binding errors, so I think that aspect can be ruled out. The data context is (in theory) being set at instantiation but I do take your point that the element that I'm after might not yet be bound at the time of the loaded event. The whole wpf binding thing is still relatively new to me so I'm not sure how To actively verify that. Back to the drawing board I guess. – Dom Sinclair Apr 02 '15 at 05:41
  • 1
    +1 for vesan's suggestion. XAML is *really* picky about the order you declare your controls in and how it binds them. Can you shift your CommandBinding to the Loaded event of the scheduler, rather than the Loaded event of the UserControl? – goobering Apr 02 '15 at 08:16
  • @vesan You were quite correct in that the scheduler had not been properly loaded / bound when the user control 'Loaded' event was being fired. Somehow I'd missed the loaded event of the scheduler control itself but thankfully when hooked up to that it works as intended. Thank you for pointing me in the correct direction. and a plus 1 for your time. – Dom Sinclair Apr 02 '15 at 12:05

0 Answers0