Я изменил код, которым вы поделились. Я предлагаю вам использовать привязку данных вместо FlipView_SelectionChanged (), всякий раз, когда элемент, добавленный в ModelItems, обновляет элемент, с которым он связан. Я надеюсь, что это будет полезно.
<Grid Name="grid">
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Content="Add New FlipView Item" Click="Button_Click"/>
<DockPanel Grid.Column="1">
<TextBlock>Flipview Item Count : </TextBlock>
<TextBlock Text="{Binding ModelItems.Count}"/>
</DockPanel>
</Grid>
<FlipView Grid.Row="1" Name="flipView" ItemsSource="{Binding ModelItems,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
<FlipView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<DockPanel>
<TextBlock Text="Index:" FontSize="20"/>
<TextBlock Text="{Binding Index}" FontSize="60" />
</DockPanel>
<DockPanel>
<TextBlock Text="Name:" FontSize="20"/>
<TextBlock Text="{Binding Name}" FontSize="60" />
</DockPanel>
</StackPanel>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>
</Grid>
Код логической части
//Interaction logic for MainWindow.xaml
public partial class MainWindow : Window
{
public BaseViewModel ViewModel { get; set; } = new BaseViewModel();
public MainWindow()
{
InitializeComponent();
this.DataContext = ViewModel;
}
static int k = 3;
private void Button_Click(object sender, RoutedEventArgs e) //can also implement using ICommand instead of event
{
this.ViewModel.ModelItems.Add(new BaseModelItem { Index = k, Name = "Name" + ++k });
}
}
//--------Model------------
public class NotifyPropertyChanged : System.ComponentModel.INotifyPropertyChanged
{
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
public void OnPropertyRaised(string propertyname)
{
PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(propertyname));
}
}
public class BaseModelItem : NotifyPropertyChanged
{
string _name = string.Empty;
public string Name
{
get { return _name; }
set { _name = value; OnPropertyRaised("Name"); }
}
int _index = 0;
public int Index
{
get { return _index; }
set { _index = value; OnPropertyRaised("Index"); }
}
}
//--------ViewModel------------
public class BaseViewModel:NotifyPropertyChanged
{
System.Collections.ObjectModel.ObservableCollection<BaseModelItem> _modelItems = new System.Collections.ObjectModel.ObservableCollection<BaseModelItem>();
public System.Collections.ObjectModel.ObservableCollection<BaseModelItem> ModelItems
{
get { return _modelItems; }
set { _modelItems = value; OnPropertyRaised("ModelItems"); }
}
public BaseViewModel()
{
ModelItems = new System.Collections.ObjectModel.ObservableCollection<BaseModelItem>();
ModelItems.Add(new BaseModelItem() { Name = "Name 1", Index = 0 });
ModelItems.Add(new BaseModelItem() { Name = "Name 2", Index = 1 });
ModelItems.Add(new BaseModelItem() { Name = "Name 3", Index = 2 });
}
}