My requirement is that i have use a flipview in my application and add pages dynamically.i want to do that when i click on addpages button my current page remove from flipview and new page comes . my code for 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;
}
A very convenoent way to do this work is to build a structure in the DataTemplate
for each item of the FlipView
and then just use ObservableCollection
to add, remove, refresh the items of the FlipView
.
<FlipView x:Name="flipView" ItemsSource="{x:Bind flipviewCollection}">
<FlipView.ItemTemplate>
<DataTemplate>
<Canvas>
<Image Source="{Binding ImageSource}" Stretch="None"/>
</Canvas>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>
Define a model for the data you need to bind in the DataTemplate
:
public class ImageSourceClass
{
public string ImageSource { get; set; }
}
Then in the code behind of your FlipView
, create a collection of this data model and add data to this collection:
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" });
}
At last if you want to remove an item and add a new one in the Button click event:
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);
}
Problem is, if you didn't use this method to build a FlipView
, you just added items to FlipView
manually, you need also manully remove one like in your code just for example here:
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);
}