views:

36

answers:

2

In WPF application I use Textbox with custom style in which ContextMenu is overriden like this:

  <Style TargetType="{x:Type TextBox}">
    <Setter Property="ContextMenu">
      <ContextMenu>
        <MenuItem Header="Copy"/>
      </ContextMenu>
    </Setter>
  </Style>

This works perfectly until I'll run window with TextBox in different threads like this:

Thread thread = new Thread(()=>
                                {
                                TestWindow wnd = new TestWindow();
                                wnd.ShowDialog();
                                });
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();

But this causes InvalidOperationException "The calling thread cannot access this object because a different thread owns it.".

How to avoid this problem?