简体   繁体   中英

Caliburn.Micro - Binding a button in a sidebar to a method in a ViewModel

I have a problem with binding a button located in a sidebar in my windows phone app. It seems like the buttons binding just dissapears..

Here's my code at the moment

 <Grid x:Name="LayoutRoot" Background="Transparent">

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

        <sidebar:SidebarControl x:Name="sidebarControl"
                            HeaderText="WP"
                            HeaderBackground="YellowGreen"
                            HeaderForeground="White"
                            SidebarBackground="{StaticResource PhoneChromeBrush}">

            <sidebar:SidebarControl.SidebarContent>
                <Grid  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="380">


                    <Button Content="Go to page 2"  x:Name="GoToPage2"/>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>

                </Grid>


            </sidebar:SidebarControl.SidebarContent>

            <Grid VerticalAlignment="Top" HorizontalAlignment="Stretch"
              Margin="12">
                <TextBlock Style="{StaticResource PhoneTextNormalStyle}">Your current view goes here</TextBlock>
            </Grid>
        </sidebar:SidebarControl>



    </Grid>

</Grid>

At the moment I am using a nuget for the sidebar called SidebarWP8. Maybe Calinbrun.Micro doesnt work with this? Or do I have to insert a binding to the VM in a grid?

Here's the method in the ViewModel:

private readonly INavigationService navigationService;

public MainPageViewModel(INavigationService navigationService)
{
  this.navigationService = navigationService;
}

public void GoToPage2()
{
   navigationService.UriFor<Page2ViewModel>().Navigate();
}

I checked the source for Caliburn Micro: I handles the convention binding by traversing the visual tree to find elements with a name. But it only checks the default Content property for each control.

That means it will only go through the Content or Children elements and not Custom properties like SidebarContent , so named elements will not be found there.

You have to wire it up by hand (by binding a command or adding a click handler).

<Button cm:Message.Attach="[Event Click] = [Action GoToPage2()]" />

这应该起作用,因为相对于默认控件而言,另一个注释器是正确的...自定义控件需要添加更多处理,这可能会增加对接的痛苦,但是使用CM上方的快捷键将查找该属性在按钮中进行相应的处理。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM