簡體   English   中英

Windows Phone-自定義控件如何將事件傳播到ViewModel

[英]Windows Phone - custom control how propagate event to ViewModel

我正在為菜單創建自定義控件,並且控件中有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我猜問題出在自定義控件中的綁定命令上,但是我不知道如何解決它。

您只需要綁定到UserControl -使用“ RelativeSource Self”將綁定到Button,這不是您想要的。 您應該能夠使用“ ElementName”綁定來找到用戶控件:

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

    ...

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

    ...

</UserControl>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM