【问题标题】:Windows Phone - custom control how propagate event to ViewModelWindows Phone - 自定义控制如何将事件传播到 ViewModel
【发布时间】:2014-10-23 13:30:57
【问题描述】:

我正在为菜单创建自定义控件,并且我的控件中有 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。

编辑2 我猜问题出在自定义控件中的绑定命令,但我不知道如何解决。

【问题讨论】:

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


    【解决方案1】:

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

    <UserControl x:Name="UserControlName" ... >
    
        ...
    
            <Button Content="{Binding Title}" 
                    Command="{Binding ElementName=UserControlName,Path=ListButtonClickCommand}"
                    CommandParameter="{Binding Url}"/>
    
        ...
    
    </UserControl>
    

    【讨论】:

      猜你喜欢
      • 2012-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多