/// <summary> /// Handle Add Translation button click. /// </summary> private void OnAddTranslationBtn_Click(object sender, RoutedEventArgs e) { var dlg = new TranslationsEditDlg() { Owner = this }; if (dlg.ShowDialog() == true) { var trans = new Translation { Index = translations.Count > 0 ? translations.Max(o => o.Index) + 1 : 1, Part = dlg.Translation.Value.partOfSpeech, Text = dlg.Translation.Value.translation, }; //To save items visible order in case if not all translations are shown in the dialog translations.Insert(countOfShownTranslations, trans); AddTranslationRow(trans); UpdateTranslationsArrowsState(); ChangesHaveBeenMade(); } }
/// <summary> /// Add new translation row (a panel with respective elements) to the dialog. /// </summary> private void AddTranslationRow(Translation tr) { var copy = (FrameworkElement)XamlReader.Parse(XamlWriter.Save(translationRow)); int addButtonIdx = translationsPanel.Children.IndexOf(addTranslationBtn); copy.Name = null; //To show that it's a new item copy.Visibility = Visibility.Visible; copy.Tag = tr; var newTranslationLbl = (TextBlock)copy.FindName(nameof(translationLbl)); newTranslationLbl.Text = TranslationConverter.ConvertToString(tr); newTranslationLbl.MouseUp += (s, e) => { //Edit the translation var dlg = new TranslationsEditDlg(tr.Text, tr.Part) { Owner = this }; if (dlg.ShowDialog() == true) { tr.Text = dlg.Translation.Value.translation; tr.Part = dlg.Translation.Value.partOfSpeech; newTranslationLbl.Text = TranslationConverter.ConvertToString(tr); ChangesHaveBeenMade(); } }; var newRemoveBtn = (Button)copy.FindName(nameof(trRemoveBtn)); newRemoveBtn.Click += (s, e) => { //Remove the translation LooseHeight(); translationsPanel.Children.Remove(copy); translations.Remove(tr); FixHeight(); countOfShownTranslations--; UpdateAddButtonState(); UpdateTranslationsArrowsState(); ChangesHaveBeenMade(); }; var newTrUpBtn = (Button)copy.FindName(nameof(trUpBtn)); newTrUpBtn.Click += (s, e) => MoveTranslationRow(copy, moveUp: true); var newTrDownBtn = (Button)copy.FindName(nameof(trDownBtn)); newTrDownBtn.Click += (s, e) => MoveTranslationRow(copy, moveUp: false); LooseHeight(); translationsPanel.Children.Insert(addButtonIdx, copy); if (Visibility == Visibility.Visible) //Only if form is already shown and knows its size { FixHeight(); } countOfShownTranslations++; UpdateAddButtonState(); }