0

我已經爲我的通用應用程序構建了類似這樣的類。目前在WP8.1部分工作。usercontrol-WP8.1中的空datacontext C#

下面的類放在共享代碼中。 (希望用它在Win8.1)

  • FolderItemViewer.xaml(用戶控件)它是DataTemplate中在MainPage.xaml中
  • FolderCollection類一個ListView,它是被綁定到ListView集合WP

現在的問題是,我已經將操縱事件連接到FolderItemViewer.xaml中的數據模板網格,以捕獲左右滑動並且它正在工作。現在基於這個,我需要更新FolderCollection類中的CollectionItem,並因此更新Mainpage.xaml中的ListView。

  1. 如何捕獲ListView項或由於操作事件位於FolderItemViewer類中的collectionitem邊界?
  2. 我可以得到listview項嗎?或者listlivew項目模板的回調函數已更改?或類似的事情?

編輯

對不起把這麼多的代碼。但是,真的很感謝有人幫助我們做到這一點。

這是FolderItemViewer.xaml

<UserControl 
x:Class="JusWrite2.FolderItemViewer" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:JusWrite2" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d"> 
<Grid x:Name="MainGrid"> 
    <Grid Height="60" Width="380" Margin="0,0,0,1"> 
     <Grid x:Name="ItemGrid" HorizontalAlignment="Left" VerticalAlignment="Center" Width="380" Height="60" Background="Transparent" Canvas.ZIndex="2" 
             ManipulationMode="TranslateX,System" ManipulationStarted="On_ChannelItem_ManipulationStarted" ManipulationDelta="On_ChannelItem_ManipulationDelta" ManipulationCompleted="OnChannelItemManipulationCompleted"> 
      <TextBlock x:Name="titleTextBlock" Margin="20,0,0,0" Canvas.ZIndex="2" VerticalAlignment="Center" TextAlignment="Left" FontSize="25" > 
      </TextBlock> 
     </Grid> 
     <Grid x:Name="DelGrid" Opacity="0.0" HorizontalAlignment="Right" VerticalAlignment="Center" Height="60" Background="Red" Canvas.ZIndex="-1" Tapped="On_ChannelDelete_Tap" Width="380"> 
      <Button Content="X" FontSize="25" Canvas.ZIndex="-1" VerticalAlignment="Center" HorizontalAlignment="Center" Width="380" BorderThickness="0" /> 
     </Grid> 
    </Grid> 
</Grid> 
</UserControl> 

代碼背後

private void OnChannelItemManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e) 
    { 
     Grid ChannelGrid = (Grid)sender; 

     Grid mGrid = (Grid)(ChannelGrid.Parent); 

     Grid DeleteGrid = (Grid)((Grid)(ChannelGrid.Parent)).Children[1]; 

     FolderCollection swipedItem = ChannelGrid.DataContext as FolderCollection;// grid has null value for datacontext 

     double dist = e.Cumulative.Translation.X; 
     if (dist < -100) 
     { 
      // Swipe left 
     } 
     else 
     { 
      // Swipe right 
     } 

    } 

FolderCollection.xaml在它有兩個班。 FolderItem和FolderCollection

public class FolderItem : INotifyPropertyChanged 
    { 
     // variables 

    public FolderItem() 
    { 

    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     if (this.PropertyChanged != null) 
     { 
      this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    public int CompletionStatus 
    { 
     //code 
    } 

    public int Priority 
    { 
     //code 
    } 

    public string FolderText 
    { 
     //code 
    } 

    public int PenColor 
    { 
     //code 
    } 

    public string UUID 
    { 
     //code 
    } 

    public string CreateUUID() 
    { 
     //code 
    } 
} 

public class FolderCollection : IEnumerable<Object> 
{ 
    private ObservableCollection<FolderItem> folderCollection = new ObservableCollection<FolderItem>(); 

    private static readonly FolderCollection instance = new FolderCollection(); 

    public static FolderCollection Instance 
    { 
     get 
     { 
      return instance; 
     } 

    } 

    public IEnumerator<Object> GetEnumerator() 
    { 
     return folderCollection.GetEnumerator(); 
    } 

    IEnumerator IEnumerable.GetEnumerator() 
    { 
     return GetEnumerator(); 
    } 

    public void Add(FolderItem fItem) 
    { 
     folderCollection.Add(fItem); 
    } 


    public ObservableCollection<FolderItem> FolderCollectionInstance 
    { 
     get 
     { 
      return folderCollection; 
     } 
    } 
} 

這是MainPage.xaml,我有數據綁定。背後

//Constructor 
    FolderListView.DataContext = fc.FolderCollectionInstance; 
    FolderListView.ItemsSource = fc.FolderCollectionInstance; 


    private void ItemListView_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args) 
    { 
     FolderItemViewer iv = args.ItemContainer.ContentTemplateRoot as FolderItemViewer; 

     if (args.InRecycleQueue == true) 
     { 
      iv.ClearData(); 
     } 
     else if (args.Phase == 0) 
     { 
      iv.ShowPlaceholder(args.Item as FolderItem); 

      // Register for async callback to visualize Title asynchronously 
      args.RegisterUpdateCallback(ContainerContentChangingDelegate); 
     } 
     else if (args.Phase == 1) 
     { 
      iv.ShowTitle(); 
      args.RegisterUpdateCallback(ContainerContentChangingDelegate); 
     } 
     else if (args.Phase == 2) 
     { 
      //iv.ShowCategory(); 
      //iv.ShowImage(); 
     } 

     // For imporved performance, set Handled to true since app is visualizing the data item 
     args.Handled = true; 
    } 

    private TypedEventHandler<ListViewBase, ContainerContentChangingEventArgs> ContainerContentChangingDelegate 
    { 
     get 
     { 
      if (_delegate == null) 
      { 
       _delegate = new TypedEventHandler<ListViewBase, ContainerContentChangingEventArgs>(ItemListView_ContainerContentChanging); 
      } 
      return _delegate; 
     } 
    } 
    private TypedEventHandler<ListViewBase, ContainerContentChangingEventArgs> _delegate; 

回答

0

// Resources 
    <DataTemplate x:Key="StoreFrontTileTemplate"> 
     <local:FolderItemViewer /> 
    </DataTemplate> 

    <ListView x:Name="FolderListView" ItemsSource="{Binding}" 
        SelectionMode="None" 
        ItemTemplate="{StaticResource StoreFrontTileTemplate}" 
        ContainerContentChanging="ItemListView_ContainerContentChanging"> 
    </ListView> 

代碼列表項應該作爲網格的數據上下文:ChannelGrid.DataContext

+0

DataContext爲空:( – alfah 2014-10-20 10:55:23

+0

我在問題中添加了FolderItemViewer.xaml的xaml代碼 – alfah 2014-10-20 11:27:55

+0

然後數據綁定出現問題,如果您在列表框中使用它,那麼FolderItemViewer.xaml應該只是一個DataTemplate – 2014-10-20 13:19:57