0

Is there a way, to get notified when an InlineUIContainer gets deleted in a RichTextBox? Currently I am using the Unload event, which is a problem because the event is also called when I switch between tabs.

My code:

Creating the InlineUIContainer:

InlineUIContainer container = new InlineUIContainer(presenter) { BaselineAlignment = BaselineAlignment.TextBottom };
container.Tag = new TagTextBoxObject(Id, InputText);
container.Unloaded += presenter_Unloaded;

The event, which should not get fired on switching tabs:

void presenter_Unloaded(object sender, RoutedEventArgs e)
{
    Dispatcher.Invoke(
        (Action)delegate()
        {
            TagTextBoxObject item = (TagTextBoxObject)(sender as InlineUIContainer).Tag;

            if (newItems.ContainsKey(item.Id))
            {
                newItems.Remove(item.Id);
            }

            if (!deletedItems.ContainsKey(item.Id))
            {
                deletedItems.Add(item.Id, item.Text);
            }
        });
}
nobody
  • 19,814
  • 17
  • 56
  • 77
BendEg
  • 20,098
  • 17
  • 57
  • 131
  • The event is called because the previously selected page was unloaded. What is the purpose to be noticed when an InlineUIContainer gets deleted? – Yoh Deadfall Feb 26 '15 at 14:10
  • I've developed a Tag based tagbox, and if i delete a tag via backspace, i delete the tag also from the database. But now the data were deleted without using backspace :) – BendEg Feb 26 '15 at 14:11
  • You should put your solution in the answer box and accept it, and not edit it into the question, otherwise the question stays in the "unsolved" queue. – Brian Tompsett - 汤莱恩 Jul 05 '15 at 13:16
  • @BrianTompsett-汤莱恩 If you're going to bump a months-old question, you should be fixing **everything** that can be fixed. There were many typos and other issues in the question that you could have fixed. – nobody Jul 06 '15 at 23:09

2 Answers2

1

You can unsubscribe from the Unload event when the TabControl.SelectionChanged is fired. And subscribe again when the specific tab is selected.

But I think that better way is to create a custom control which will hosts a TextBox and an ItemsControl and do not use RichTextBox.

Yoh Deadfall
  • 2,711
  • 7
  • 28
  • 32
  • Thank you for your unswer, but i found a more handy solution, look at my **EDIT 2**. But you gave me the hint :) – BendEg Feb 26 '15 at 14:44
0

The solution, look wether the parent is loaded:

    void presenter_Unloaded(object sender, RoutedEventArgs e)
    {
        if (this.Parent != null && this.Parent is FrameworkElement)
        {
            if ((this.VisualParent as FrameworkElement).IsLoaded)
            {
                Dispatcher.Invoke(
                    (Action)delegate()
                    {
                        TagTextBoxObject item = (TagTextBoxObject)(sender as InlineUIContainer).Tag;

                        if (newItems.ContainsKey(item.Id))
                        {
                            newItems.Remove(item.Id);
                        }

                        if (!deletedItems.ContainsKey(item.Id))
                        {
                            deletedItems.Add(item.Id, item.Text);
                        }
                    });
            }
        }
    }
BendEg
  • 20,098
  • 17
  • 57
  • 131