Windows Phone - 自定义控制如何将事件传播到ViewModel

时间:2014-10-23 13:30:57

标签: c# xaml windows-phone-8 mvvm mvvm-light

我正在为菜单创建自定义控件,我在我的控件中有ListBox。像这样:

<ListBox x:Name="MenuItemsList"
         Grid.Row="1"
         ItemsSource="{Binding ProgramList, Mode=OneWay}">
         <ListBox.ItemTemplate>
             <DataTemplate>
                 <StackPanel Margin="10">
                       <TextBlock Text="{Binding Title}" />
                  </StackPanel>
             </DataTemplate>
         </ListBox.ItemTemplate>
 </ListBox>

现在,当我想要捕获Tap事件时,从类中获取属性并保留MVVM模型。如何在MainPage.xaml.cs或MainViewModel中捕获它?

我在MainPage.xaml中有这段代码:

<controls:BottomMenu x:Name="BottomMenu" Canvas.Top="{Binding MenuCanvasTop}"
                             Width="480" Height="400">
</controls:BottomMenu>

我已在MainViewModel中准备此代码:

public RelayCommand<string> GoToSectionCommand
        {
            get
            {
                return goToArticleCommand
                    ?? (goToArticleCommand = new RelayCommand<string>(
                         NavigateToSection));
            }
        }

但我不知道怎么称呼它。什么是最好的方式?

修改 我试图扩展列表框:

 <ListBox x:Name="MenuItemsList"
                                     Grid.Row="1"
                                     ItemsSource="{Binding ProgramList, Mode=OneWay}">
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel Margin="10">
                                            <Button Content="{Binding Title}" 
                                                    Command="{Binding ListButtonClickCommand, Source={RelativeSource Self}}"
                                                    CommandParameter="{Binding Url}"/>
                                        </StackPanel>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>

使用代码隐藏:

    public ICommand ListButtonClickCommand
    {
        get { return (ICommand)GetValue(ListButtonClickCommandProperty); }
        set { SetValue(ListButtonClickCommandProperty, value); }
    }

    public static readonly DependencyProperty ListButtonClickCommandProperty =
            DependencyProperty.Register("ListButtonClickCommand", typeof(ICommand), typeof(BottomMenu), new PropertyMetadata(null)); 

    public BottomMenu()
    {
        InitializeComponent();
    }

然后在MainPage.xaml:

    <controls:BottomMenu x:Name="BottomMenu" Canvas.Top="{Binding MenuCanvasTop}"
                         Width="480" Height="400"
                         ListButtonClickCommand="{Binding MenuItemButtonCommand}">

    </controls:BottomMenu>

在MainViewModel中:

    private ICommand menuItemButtonCommand;
    public ICommand MenuItemButtonCommand
    {

        get
        {
            return menuItemButtonCommand
                ?? (menuItemButtonCommand = new RelayCommand(
                     NavigateToArticleSection));
        }

    }

现在没有运气。它不起作用。不会触发RelayCommand。

EDIT2 我想问题是自定义控件中的绑定命令,但我不知道如何解决它。

1 个答案:

答案 0 :(得分:1)

你只需要绑定到UserControl - 使用“RelativeSource Self”将绑定到Button,这不是你想要的。您应该能够使用“ElementName”绑定来定位用户控件:

<UserControl x:Name="UserControlName" ... >

    ...

        <Button Content="{Binding Title}" 
                Command="{Binding ElementName=UserControlName,Path=ListButtonClickCommand}"
                CommandParameter="{Binding Url}"/>

    ...

</UserControl>