2017-08-14 4 views
0

Si vous pouvez gagner du temps, je travaille sur un problème pour lequel je ne trouve pas de solution sur Internet.WPF: Modifier les onglets fait disparaître Windowsformshost

J'ai besoin de richtextboxes de deux onglets pour lier la même propriété. Les deux RichtextBox sont hébergés dans WPF via Windowsformshost. Mais si j'alterne entre les onglets, un RichtTextBox va simplement disparaître (toujours le premier qui était visible). Je migre une application et jusqu'à présent, je suis forcé pour utiliser la RichtextBox Windowsforms.

J'espère avoir réussi à transmettre correctement mon problème - désolé, je ne suis pas un locuteur natif.

Merci à l'avance

Edit: on m'a demandé de fournir un exemple clair de mon problème. Merci pour la note. J'ai complètement réécrit ma question. En outre, j'ai uploaded a micro app où j'ai isolé le problème. Cliquez simplement sur les deux boutons de l'onglet alternativement et une Richtextbox disparaîtra.

Ci-dessous, je vais fournir le code si cela sert:

Ceci est mon 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" /> 

C'est son 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)); 
    } 
} 

De plus, je deux pages (MyPageLeft, MyPageRight) qui utilisent le même viewmodel (TabViewModel) et utilisent le même bit de code XAML:

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

Les deux pages utilisent la même 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)); 
    } 
} 

Le problème: Si je commence l'application et cliquez sur les deux boutons de l'onglet en alternance, l'un des RichtextBoxes encadrés disparaitra.

+0

Veuillez toujours fournir un exemple minimal, complet et vérifiable lorsque vous posez une question sur SO: https://stackoverflow.com/help/mcve – mm8

Répondre

0

Si quelqu'un pouvait en avoir besoin, j'ai utilisé une solution sale - bien que ce ne soit pas recommandé.

J'ai prolongé l'événement des boutons de l'onglet de commutation. Il prend la propriété RTF de la Richtextbox du Tab sélectionné et l'injecte dans l'autre Richtextbox. Il va un peu comme ceci:

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

Notez que ce hack est un débutant sur une approche globale probablement discutable.

Merci à tous ceux qui ont lu ma question!