簡體   English   中英

我想在用戶更改頁面時從flipview中刪除項目。 有什么我能做的嗎?

[英]I want to remove an item from flipview when the user change pages. Is there anything I can do?

我的要求是我在我的應用程序中使用了flipview並動態添加頁面。我希望這樣做,當我點擊addpages按鈕時,我的當前頁面從flipview刪除並且新頁面出現。 我的addpage代碼

   public void Add_Pages_Click(object sender, RoutedEventArgs e)
    {
        try
        {

            my_canvas = new Canvas();
            my_canvas.Name = "Item" + DateTime.Now.ToFileTime().ToString();
            //fp1.ItemsSource = fi;
            fp1.Items.Add(my_canvas);
            fp1.SelectionChanged += Fp1_SelectionChanged;
            Stickers1.Visibility = Visibility.Collapsed;
            Backgrounds1.Visibility = Visibility.Collapsed;
            Popup_wordart.IsOpen = false;
            PopUp_Media.IsOpen = false;
            my_canvas.Visibility = Visibility.Collapsed;
            Audio_Recorder.IsOpen = false;
        }
        catch (Exception ex)
        {


        }
       // Backward_Arrow.Visibility = Visibility.Visible;
    }

一種非常方便的方法是在DataTemplateFlipView每個項目構建一個結構,然后使用ObservableCollection來添加,刪除,刷新FlipView的項目。

<FlipView x:Name="flipView" ItemsSource="{x:Bind flipviewCollection}">
    <FlipView.ItemTemplate>
        <DataTemplate>
            <Canvas>
                <Image Source="{Binding ImageSource}" Stretch="None"/>
            </Canvas>
        </DataTemplate>
    </FlipView.ItemTemplate>
</FlipView>

為需要在DataTemplate綁定的數據定義模型:

public class ImageSourceClass
{
    public string ImageSource { get; set; }
}

然后在FlipView的代碼后面,創建此數據模型的集合並將數據添加到此集合:

ObservableCollection<ImageSourceClass> flipviewCollection = new ObservableCollection<ImageSourceClass>();

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    flipviewCollection.Clear();
    flipviewCollection.Add(new ImageSourceClass { ImageSource = "Assets/1.jpg" });
    flipviewCollection.Add(new ImageSourceClass { ImageSource = "Assets/2.jpg" });
    flipviewCollection.Add(new ImageSourceClass { ImageSource = "Assets/3.jpg" });
    flipviewCollection.Add(new ImageSourceClass { ImageSource = "Assets/4.jpg" });
    flipviewCollection.Add(new ImageSourceClass { ImageSource = "Assets/5.jpg" });
}

最后,如果要刪除項目並在Button click事件中添加新項目:

private void Button_Click(object sender, RoutedEventArgs e)
{
    //index = flipView.SelectedIndex + 1 because when remove one item, next item will be shown, 
    //it will behavior like a new item replace the old one.
    flipviewCollection.Insert(flipView.SelectedIndex + 1, new ImageSourceClass { ImageSource = "Assets/new.jpg" });
    flipviewCollection.Remove(flipView.SelectedItem as ImageSourceClass);                                
}

問題是,如果您沒有使用此方法來構建FlipView ,您只需手動將項目添加到FlipView ,您還需要在代碼中手動刪除一個,例如:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var my_canvas = new Canvas();
    var textbox = new TextBox();
    my_canvas.Children.Add(textbox);
    flipView.Items.Insert(flipView.SelectedIndex + 1, my_canvas);
    flipView.Items.RemoveAt(flipView.SelectedIndex);              
}

暫無
暫無

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

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