ISerialized .Net, C#, Scrum and agile software development

21Oct/100

Sending parameters with Caliburns ActionMessage

For the last two days we have been struggling with a rather peculiar problem in a Caliburn solution. In a ResourceDictionary we had defined a Popup with a ListBox containing some textblocks and buttons. The buttons are bound to a Presenter (Caliburn v1.1), and we tried to pass a parameter with the Message.

The parameter we tried to pass was also data bound in a TextBlock right next to the button, but due to the way we had defined the ListBox in a dictionary, we ended up with the following error, when trying to give the TextBlock a name:

Cannot set Name attribute value 'test' on element 'TextBlock'. 'TextBlock' is under the scope of element 'TestPopup', which already had a name registered when it was defined in another scope. UDir.Lkt.Client

As seen here we are obvisously not the only one struggling with this, and based on the facts that Microsoft has not solved this problem even though it has been there for quite a while and have postponed it, it will probably never be fixed :(

If we had been able to give the TextBlock a name, we could have used the following syntax on the Button:

<TextBlock Name="test" Text="{Binding MyText}"/>
<Button cal:Message.Attach="MyMethod(test.Text)">

But due to the fact that we didn't have the possibility to give the TextBlock a name, we had to look for solutions elsewhere.  A quick search on Google, shows that there are allot of people having the same problem, most of them suggesting to solve the problem in code-behind or rewrite to use DataGrid instead of ListBox. This however was no feasible solution for us!

Caliburn have a much better solution for this :

<Button>
    <cal:Message.Triggers>
        <cal:RoutedMessageTriggerCollection>
            <cal:EventMessageTrigger EventName="Click">
                <cal:EventMessageTrigger.Message>
                    <cal:ActionMessage MethodName="MyMethod">
                        <cal:Parameter Value="{Binding MyText}"></cal:Parameter>
                    </cal:ActionMessage>
                </cal:EventMessageTrigger.Message>
            </cal:EventMessageTrigger>
        </cal:RoutedMessageTriggerCollection>
    </cal:Message.Triggers>
</Button>

There are numerous situations where passing parameters from the View in Caliburn and WPF might be interesting, this one solves most of them!

See the rest of my posts on Caliburn here

Posted by Pål Eie

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.