0

if you can spare the time, I am working on a problem for which I can't find a solution on the internet.

I need two tabs' richtextboxes to bind the same property. Both RichtextBoxes are hosted in WPF via Windowsformshost. But if I alternate between tabs, one RichtTextBox will simply dissapear (always the first one that was visible). I am migrating an app and so far, I am forced to use the Windowsforms RichtextBox.

I hope I managed to properly convey my problem - sorry, I am not a native speaker.

Thanks in advance

Edit: I was asked to provide a clear example of my problem. Thanks for the note. I completely rewrote my question. Further, I have uploaded a micro app where I have isolated the problem. Just click the two tab buttons alternately and one Richtextbox will dissapear.

Below, I will provide the code if this serves:

This is my Mainwindow (XAML):

<StackPanel Orientation="Horizontal" Height="35" Margin="0,35,0,0" VerticalAlignment="Top" >
        <Button x:Name="Tab1" Command="{Binding LeftCommand}" Content="Left" MinWidth="100" />
        <Button x:Name="Tab2" Command="{Binding RightCommand}" Content="Right" MinWidth="100" />
    </StackPanel>
        <Frame x:Name="MyFrame" 
               Content="{Binding Path=CurrentTab, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
               Margin="5,70,0,0" NavigationUIVisibility="Hidden"  />   

This is its viewmodel:

class MainWindowViewModel : INotifyPropertyChanged
{
    public ICommand LeftCommand { get; }
    public ICommand RightCommand { get; }

    private TabViewModel MyTabViewModel { get; set; }
    private PageLeft MyPageLeft { get; set; }
    private PageRight MyPageRight { get; set; }

    public MainWindowViewModel()
    {
        this.LeftCommand = new ModelCommand(p => this.SetSelectedTab("left"));
        this.RightCommand = new ModelCommand(p => this.SetSelectedTab("right"));

        this.MyTabViewModel = new TabViewModel();

        this.MyPageLeft = new PageLeft() { DataContext = this.MyTabViewModel };
        this.MyPageRight = new PageRight() { DataContext = this.MyTabViewModel };

        //initial view on something
        //this.SetSelectedTab("left");
    }

    private void SetSelectedTab(string param)
    {
        switch (param)
        {
            case "left":
                this.CurrentTab = this.MyPageLeft;
                break;
            case "right":
                this.CurrentTab = this.MyPageRight;
                break;
        }
    }

    private object _CurrentTab;
    public object CurrentTab
    {
        get { return _CurrentTab; }
        set
        {
            if (value != _CurrentTab)
            {
                _CurrentTab = value;
                NotifyPropertyChanged_MainViewModel();
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    // This method is called by the Set accessor of each property.  
    // The CallerMemberName attribute that is applied to the optional propertyName  
    // parameter causes the property name of the caller to be substituted as an argument.  
    private void NotifyPropertyChanged_MainViewModel([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Furthermore, I have two pages (MyPageLeft, MyPageRight) that use the same viewmodel (TabViewModel) and use the same bit of XAML code:

<ContentControl Content="{Binding Path=MyWindowsFormsHost, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

Both Pages use the same TabViewModel:

class TabViewModel : INotifyPropertyChanged
{
    private WindowsFormsHost _MyWindowsFormsHost;

    public WindowsFormsHost MyWindowsFormsHost
    {
        get { return _MyWindowsFormsHost; }
        set
        {
            if (value != _MyWindowsFormsHost)
            {
                _MyWindowsFormsHost = value;
                NotifyPropertyChanged_TabViewModel();
            }
        }
    }

    public TabViewModel()
    {
        this.MyWindowsFormsHost = new WindowsFormsHost() { Child = new RichTextBox() { Text = "test" } };
    }

    public event PropertyChangedEventHandler PropertyChanged;

    // This method is called by the Set accessor of each property.  
    // The CallerMemberName attribute that is applied to the optional propertyName  
    // parameter causes the property name of the caller to be substituted as an argument.  
    private void NotifyPropertyChanged_TabViewModel([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

The Problem: If I start the app and click on the two tab buttons alternatingly, one of the framed RichtextBoxes will dissapear.

Apfelsaft23
  • 316
  • 2
  • 23
  • Please always provide a Minimal, Complete, and Verifiable example when you ask a question on SO: https://stackoverflow.com/help/mcve – mm8 Aug 15 '17 at 13:09

1 Answers1

0

If anyone might need it, I used a dirty solution - although it might not be recommendable.

I extended the event of the switch tab buttons. It takes the RTF property of the currently selected Tab's Richtextbox and infuses it in the other Richtextbox. It goes kinda like this:

if (Tab2 Button is clicked)
this.MyRTF = Tab1.Richtextbox.RTF;
Tab2.Richttextbox.Rtf = this.MyRTF;

Note that this is a beginner's hack on a probably overall questionable approach.

Thanks to anyone who read my question!

Apfelsaft23
  • 316
  • 2
  • 23