我的命令绑定不起作用 - MVVM / DataTemplate

时间:2013-10-15 23:47:18

标签: c# data-binding mvvm windows-phone-8 command

我正在尝试绑定到LoadSpacesCommand,它是ViewModel中我的Dashboard属性的一部分,如下所示:

    <DataTemplate x:Key="OrganisationsItemTemplate">
        <StackPanel VerticalAlignment="Top" Margin="5,0,0,0">
            <Button Command="{Binding LoadSpacesCommand}" CommandParameter="{Binding}" ...>
                <Grid Margin="0,0,5,0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="67"/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <StackPanel Grid.Column="0" Background="Transparent">
                        <Border Background="White" BorderThickness="0" Width="62" Height="62" HorizontalAlignment="Left" Margin="0,0,0,5">
                            <Image Source="{Binding image.thumbnail_link}" Width="62" Height="62"></Image>
                        </Border>
                    </StackPanel>
                    <StackPanel Grid.Column="1" VerticalAlignment="Center" Background="Transparent">
                        <TextBlock Text="{Binding name}" HorizontalAlignment="Left" FontSize="30" VerticalAlignment="Center" Margin="0,0,0,5" />
                    </StackPanel>
                </Grid>
            </Button>
        </StackPanel>
    </DataTemplate>

以下是datatemplate的工作原理:

        <phone:PanoramaItem Header="Organisations">
            <phone:LongListSelector x:Name="OrganisationList"
                                    JumpListStyle="{StaticResource OrganisationsJumpListStyle}"
                                    Background="Transparent"
                                    GroupHeaderTemplate="{StaticResource OrganisationsGroupHeaderTemplate}"
                                    ItemTemplate="{StaticResource OrganisationsItemTemplate}" 
                                    ItemsSource="{Binding Organisations}" 
                                    Margin="40,0,0,96" 
                                    LayoutMode="List"
                                    HideEmptyGroups="True"
                                    IsGroupingEnabled="True">

            </phone:LongListSelector>
            <!--<ListBox Name="Organisation" ItemTemplate="{StaticResource OrganisationTemplate}" ItemsSource="{Binding Organisations}" Margin="0,0,0,96" IsSynchronizedWithCurrentItem="False"/>-->
        </phone:PanoramaItem>

这是我的应用程序页面datacontext:

...shell:SystemTray.IsVisible="False" DataContext="{Binding Dashboard}"

我需要在命令中输入访问视图模型命令的任何想法吗?

1 个答案:

答案 0 :(得分:2)

您可以使用LongListSelector绑定回ElementName,而DataContext应该是ViewModel

Command="{Binding Path=DataContext.LoadSpacesCommand, ElementName=OrganisationList}"