I try to use the Caliburn.Micro
framework to optimize my current WPF
application following the MVVM
scheme. Setting the action of a button can normally achieved very without any problem just by setting the x:Name
value of the button and adding a function with the same name in the view model. Things (at least it seems to me so) get more complicated when you try to do this inside a ControlTemplate
. My current approach looks like this code:
<ItemsControl x:Name="Chains">
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl">
<StackPanel>
<ContentControl/>
<ItemsPresenter />
<Button Content="+" x:Name="AddChain"/>
</StackPanel>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
But sadly, the AddChain
method of the ViewModel
which corresponds to the view
in which this code is placed is never called.
I think that I have to tell Caliburn
to use this ViewModel
and not a ViewModel
which corresponds to the ControlTemplate
. Is this correct? How can I solve the issue?
Bind the cal:Bind.Model
attached property to your view model:
<ItemsControl x:Name="Chains">
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl" xmlns:cal="http://www.caliburnproject.org">
<StackPanel cal:Bind.Model="{Binding}">
<ContentControl/>
<ItemsPresenter />
<Button Content="+" x:Name="AddChain"/>
</StackPanel>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
There are more examples under the CaliburnMicro. I had the same issue with buttons created dynamically under a ItemSource. MM8 answer answer is helpfull and shall be enough, but if you looking to know more or for other applications have a read through will help you. CaliburnMicro ActionDocs - See Section "Action Parameters"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.