Windows Phone - 将UserControl的按钮绑定到ViewModel

时间:2014-05-02 19:55:38

标签: wpf xaml windows-phone-8 mvvm mvvm-light

我正在努力将UserControl(放在ListBox.ItemTemplate中)内的Button绑定到我的ViewModel。

页面A包含以下列表框:

<ListBox x:Name="lbUpcomingBetgames" ItemsSource="{Binding UpcomingBetgames}" Background="{x:Null}">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="HorizontalAlignment" Value="Stretch"/>
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                        <Setter Property="Margin" Value="0,4,0,4"/>
                    </Style>
                </ListBox.ItemContainerStyle>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <customControl:BetgameInfoControl x:Name="upcomingBetgameControl" />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

我的ViewModel包含一个我想在我的CustomControl BetgameInfoControl中绑定的RelayCommand:

public RelayCommand BetgameRegisterClicked
    {
        get;
        private set;
    }

我的CustomControl包含一个按钮:

<Button Grid.Row="0" Content="Accept" Height="50" Command="{Binding Path=Parent.DataContext.BetgameRegisterClicked, ElementName=upcomingBetgameControl}" CommandParameter="{Binding Description}" />

永远不会调用RelayCommand。我尝试了不同的方法来绑定我的Button中的Command-Property但没有任何效果。

有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

您需要更改按钮的datacontext,如此=&gt;

DataContext={Binding ElementName=lbUpcomingBetgames,path=DataContext.BetgameRegisterClicked}