Question

I have a control named ZoomSlider which I would like to reference in my EventTrigger called via the Caliburn.Micro Message.Attach functionality as such:

cal:Message.Attach="[Event PreviewMouseLeftButtonDown] = [Action ToggleNew($this, $ZoomSlider)]"

The reasoning is that I want then to take action on my UserControl (in this case, slide it open) when my ToggleNew event is raised, but it is impossible to refer to a control in my View from the ViewModel.

How can I refer to the ZoomSlider control from the above code ? My first parameter $this works perfectly but cannot have the same using the UserControl.

Was it helpful?

Solution

Let's assume that your ZoomSlider control is named myZoomSlider like this:

<ZoomSlider x:Name="myZoomSlider" />

Now you can try two things to pass this control to your ToogleNew method on your view-model, but first of all you need to define the method ToogleNew so that it takes a ZoomControl control or any of its super types as a parameter, something like this:

public void ToogleNew(ZoomSlider zoomSlider); // you can also pass any additional parameters if you like to

Now back to the two methods I told you about:

  • Try to pass the control as is like this:

    cal:Message.Attach="[Event PreviewMouseLeftButtonDown] = [Action ToggleNew(myZoomSlider)]"
    
  • If the first option didn't work for you, then you can try the following:

    <ZoomSlider x:Name="myZoomSlider" />
    <Button Content="Toogle">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <cal:ActionMessage MethodName="ToogleNew">
                    <cal:Parameter Value="{Binding ElementName=myZoomSlider}" />
                </cal:ActionMessage>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Button>
    

You can take a look at the All about actions wiki documentation to learn everything and anything regarding actions and action messages.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top