簡體   English   中英

用戶控件中的空數據上下文-WP8.1 C#

[英]Null datacontext in usercontrol- WP8.1 C#

我已經為我的通用應用構建了這樣的類。 目前在WP8.1部分中工作。

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

  • FolderItemViewer.xaml(UserControl)它是MainPage.xaml中ListView的數據模板。
  • FolderCollection類,它是綁定到WP的Mainpage.xaml中的Listview的集合。

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

  1. 由於操縱事件位於FolderItemViewer類中,我如何捕獲該listview項或綁定的collectionitem?
  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.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;

該列表項應作為網格的數據上下文可用: ChannelGrid.DataContext

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM