如何访问DataCollection

时间:2014-03-03 09:14:14

标签: c# wpf xaml windows-phone-8

我的WP8应用程序中有一个Pivot视图,我正试图在Click事件“removePin”上从视图的类中访问数据集合。 数据集合的来源是另一个名为Pins的类。

我怎样才能实现这一目标?

这是我对该特定部分的XAML的代码段

<phone:PivotItem Header="Pins">
   <!-- Content Panel -->
   <Grid x:Name="ContentPanel2" HorizontalAlignment="Left" Height="583" Margin="10,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="460">
      <Grid.ColumnDefinitions>
            <ColumnDefinition Width="400*"/>
            <ColumnDefinition Width="0*"/>
            <ColumnDefinition Width="87*"/>
      </Grid.ColumnDefinitions>
      <ListBox x:Name="lstData2" ItemsSource="{Binding DataCollection2, Source={StaticResource PinsCollection}}" 
       Grid.ColumnSpan="3" Foreground="#FF1D53D0" Height="583" VerticalAlignment="Bottom">
          <ListBox.ItemTemplate>
            <DataTemplate>
               <StackPanel Orientation="Horizontal" Tap="StackPanel_Tap">
                  <Image Margin="8" VerticalAlignment="Top" Source="{Binding ImageUri}"
                     Width="100" Height="100" />
                  <StackPanel Height="93" Width="259" >
                      <TextBlock Margin="8" Width="250" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Left" Foreground="#FF1D53D0"
                       Text="{Binding Pinnedname}"  Height="33" RenderTransformOrigin="0.5,0.5" FontFamily="Segoe WP SemiLight" FontSize="24" FontWeight="Bold" />
                      <TextBlock Width="155" Margin="8,0,8,8" VerticalAlignment="Top"
                       HorizontalAlignment="Left" Text="{Binding Status}" Foreground="#FF1D53D0" FontFamily="Segoe WP SemiLight" />
                 </StackPanel>
              <toolkit:ContextMenuService.ContextMenu>
                 <toolkit:ContextMenu>
                    <toolkit:MenuItem Header="Remove Pin" Click="RemovePin_Click" Tag="{Binding pinId}"/>
                 </toolkit:ContextMenu>
              </toolkit:ContextMenuService.ContextMenu>
            </StackPanel>
         </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
    <!-- End of Content Panel -->
  </Grid>

1 个答案:

答案 0 :(得分:0)

创建自定义命令类:

public class CustomCommand : ICommand
{
    Action _exec;
    public CustomCommand(Action exec)
    {
        _exec = exec;
    }
    public void Execute(object parameter)
    {
        if (_exec != null) _exec();
    }
    public bool CanExecute(object parameter)
    {
        return true;
    }
    public event EventHandler CanExecuteChanged;
}

将它添加到视图模型中,以获取针脚集合的元素(我认为它是PinItem)

public CustomCommand RemovePinCommand
{
    get { return (CustomCommand)GetValue(RemovePinCommandProperty); }
    set { SetValue(RemovePinCommandProperty, value); }
}
public static readonly DependencyProperty RemovePinCommandProperty =
    DependencyProperty.Register("RemovePinCommand", 
    typeof(CustomCommand), 
    typeof(PinItem), 
    new UIPropertyMetadata(null));

在该类的构造函数中,实现此命令的逻辑:

        RemovePinCommand = new CustomCommand(() =>
        {
            //delete this pin from its parent collection
        });

最后一步是将命令绑定到菜单项:

<toolkit:MenuItem Header="Remove Pin" Command="{Binding RemovePinCommand}"/>