2017-08-14 48 views
0

如果您可以腾出时间,我正在处理一个问题,我无法在Internet上找到解决方案。WPF:更改选项卡使Windowsformshost子项消失

我需要两个选项卡'richtextboxes绑定相同的属性。这两个RichtextBoxes都通过Windowsformshost在WPF中进行托管。但是如果我在选项卡之间切换,一个RichtTextBox将仅仅消失(总是第一个可见)。我正在迁移一个应用程序,到目前为止,我是强制使用Windowsforms RichtextBox。

我希望我能够正确地传达我的问题 - 对不起,我不是母语的人。

在此先感谢

编辑: 我被要求提供我的问题的一个明显的例子。谢谢你的提示。我完全重写了我的问题。此外,我有uploaded a micro app,我已经隔离了这个问题。只需交替地点击两个标签按钮,一个Richtextbox就会消失。

下面,我将提供代码,如果这个服务:

这是我的主窗口(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" /> 

这是它的视图模型:

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)); 
    } 
} 

而且,我有两页(MyPageLeft,MyPageRight)使用相同的视图模型(TabViewModel)并使用相同位的XAML代码:

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

两个页面使用相同的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)); 
    } 
} 

问题:如果我启动应用程序,并单击这两个标签按钮交替,该框架RichtextBoxes之一将dissapear。

+0

当您对SO提出问题时,请始终提供最小,完整和可验证的示例:https://stackoverflow.com/help/mcve – mm8

回答

0

如果有人可能需要它,我使用了一个肮脏的解决方案 - 虽然它可能不被推荐。

我扩展了切换标签按钮的事件。它采用当前选中的Tab的Richtextbox的RTF属性,并将其注入到其他Richtextbox中。它有点像这样:

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

请注意,这是一个初学者的黑客可能总体可疑的方法。

感谢任何阅读我的问题的人!