【问题标题】:Windows Phone - Bind Button of UserControl to ViewModelWindows Phone - 将 UserControl 的按钮绑定到 ViewModel
【发布时间】:2014-05-02 19:55:37
【问题描述】:

我正在努力将 UserControl(位于 ListBox.ItemTemplate 内)内的按钮绑定到我的 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。我尝试了不同的方法来绑定按钮中的命令属性,但没有任何效果。

有人可以帮帮我吗?

【问题讨论】:

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


【解决方案1】:

您需要像这样更改按钮的数据上下文 =>

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

【讨论】:

  • 感谢您的回答。它仍然无法正常工作。您将如何设置按钮的命令属性?
  • Oups 对不起,我失败了 ^^ 将 Datacontext={ 替换为 Command ={
  • 我之前也尝试过,但它也不起作用:(还有其他建议吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-29
  • 2016-07-11
  • 1970-01-01
  • 1970-01-01
  • 2011-07-19
  • 2014-12-08
  • 2011-10-31
相关资源
最近更新 更多