usercontrol中的空datacontext-WP8.1 C#

时间:2014-10-20 08:07:31

标签: c# windows-runtime windows-phone-8.1 datatemplate listviewitem

我为我的Universal应用程序构建了这样的类。目前正在WP8.1部分工作。

以下类放在共享代码中。 (希望在Win8.1中使用它)

  • FolderItemViewer.xaml(UserControl)它是MainPage.xaml中ListView的DataTemplate
  • FolderCollection类,它是绑定到WP的Mainpage.xaml中的Listview的集合

现在的问题是,我已将操作事件连接到FolderItemViewer.xaml中的datatemplate网格,以捕获向右和向左滑动,它正在工作。现在基于此,我需要在FolderCollection类中更新CollectionItem,从而更新Mainpage.xaml中的ListView。

  1. 如果操作事件位于FolderItemViewer类中,我如何捕获listview项或collectionitem bound?
  2. 我可以获取listview项目吗?或者listlivew项目模板的回调函数发生了变化?还是那样的?
  3. 编辑

    很抱歉收到这么多代码。但是真的很感谢有人帮助你做到这一点。

    这是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。

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

    背后的代码

        //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;
    

1 个答案:

答案 0 :(得分:0)

列表项应该可用作网格的数据上下文:ChannelGrid.DataContext