WPF:更改选项卡使Windowsformshost子项消失

时间:2017-08-14 20:37:59

标签: wpf mvvm data-binding richtextbox windowsformshost

如果你可以节省时间,我正在解决一个我无法在互联网上找到解决方案的问题。

我需要两个标签'richtextboxes来绑定同一个属性。两个RichtextBox都通过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"  />   

这是它的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));
    }
}

此外,我有两个页面(MyPageLeft,MyPageRight)使用相同的viewmodel(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));
    }
}

问题:如果我启动应用并交替点击两个标签按钮,其中一个带框的RichtextBox就会消失。

1 个答案:

答案 0 :(得分:0)

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

我扩展了切换选项卡按钮的事件。它获取当前所选Tab的Richtextbox的RTF属性,并将其注入其他Richtextbox。它有点像这样:

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

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

感谢所有阅读我问题的人!