public MainViewModel() { this.Items = new ObservableCollection<DataItem>(); for (int i = 0; i < 5; i++) { var dataItem = new DataItem() { Header = "Item " + i, Content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit." + "Nulla euismod dictum sapien dictum tempus." + "In hac habitasse platea dictumst. Curabitur ut consectetur risus." + "In neque magna, feugiat vitae ligula sit amet, condimentum tempus nulla." + "Vestibulum condimentum est quis neque tincidunt interdum." + "Nunc pellentesque orci id elit pharetra, at cursus odio tincidunt." + "Quisque vel blandit est. Vivamus in purus eget eros iaculis pulvinar." + "Aliquam erat volutpat. Nulla mi augue, molestie sit amet neque non, convallis fermentum enim." + "Mauris faucibus ante sed orci sagittis vehicula. Cras ac felis ac est imperdiet gravida non id eros." + "Vivamus odio eros, imperdiet vel lectus id, scelerisque pellentesque magna." + "Vestibulum eleifend pharetra vestibulum." + "Maecenas quis suscipit eros, nec dictum dui. In hac habitasse platea dictumst." }; this.Items.Add(dataItem); } }
private void AddItemButton_Click(object sender, RoutedEventArgs e) { var mainViewModel = this.DataContext as MainViewModel; // Any of the fields is empty - animate an error message if (String.IsNullOrEmpty(this.headerTBox.Text) || String.IsNullOrEmpty(this.contentTBox.Text)) { this.errorTBlock.Opacity = 1; var a = new DoubleAnimation { From = 1.0, To = 0.0, FillBehavior = FillBehavior.Stop, BeginTime = TimeSpan.FromSeconds(2), Duration = new Duration(TimeSpan.FromSeconds(0.8)) }; var storyboard = new Storyboard(); storyboard.Children.Add(a); Storyboard.SetTarget(a, this.errorTBlock); Storyboard.SetTargetProperty(a, new PropertyPath(OpacityProperty)); storyboard.Completed += delegate { this.errorTBlock.Opacity = 0; }; storyboard.Begin(); return; } // Otherwise add new item in the TileView DataItem dataItem = new DataItem() { Header = this.headerTBox.Text, Content = this.contentTBox.Text }; mainViewModel.Items.Add(dataItem); this.headerTBox.Text = String.Empty; this.contentTBox.Text = String.Empty; }