用户控制中的数据上下文为空-WP8.1 C#

本文关键字:-WP8 上下文 控制 数据 用户 | 更新日期: 2023-09-27 18:27:10

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

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

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

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

  1. 由于操作事件位于FolderItemViewer类中,我如何捕获该列表视图项或集合绑定
  2. 我可以获取列表视图项目吗?或者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.xml中有两个类。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;

用户控制中的数据上下文为空-WP8.1 C#

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