winforms 응용 프로그램의 wpf richtextbox

Aug 20 2020

나는 VB로 작성된 프로젝트 윈폼에서 WPF를 RichTextBox를 사용하고 싶습니다
윈폼 한 형태와 버튼으로 프로젝트 만든 I
형성 그때 컨트롤 라이브러리는 WPF에 WPF를 RichTextBox를 배치 새 프로젝트 WPF 사용자를 추가
I가 ElementHost 상호 운용성을 추가 이 수입품을 가진 WinForm

Imports System
Imports System.Windows.Forms
Imports System.Windows.Forms.Integration

여기에서 나는 10 ~ 7 세의 SO 질문 중 일부를 잃어 버렸습니다 .MS 자습서는
WPF 양식의 코드에 별로 도움이되지 않습니다.

Public Class UserControl1
  ReadOnly rtbWPF As New UserControl
  ElementHost
  wpfwindow.Show
End Class

XAML 코드를 게시하지 않았습니다. 어떻게해야할지 모르겠습니다.

그렇다면 WPF 양식을 RTB와 WinForms 양식에 연결하기 위해 다음에해야 할 일은 무엇입니까?
SQLite DB에서 WPF RichTextBox로 데이터를로드하고 RTB에 입력 한 텍스트를 DB에 저장하고 싶습니다.

답변

1 James_Duh Aug 21 2020 at 04:01

이 답변은 @KyleWang 멋진 답변을 확장하기위한 것
입니다. WPF RichTextBox의 벡터 선택에 대한 한 가지 큰 문제는 WPF RichTextBox 컨트롤에 Text 속성이 없다는 것입니다. 여기에 모든 텍스트를 가져 오는 한 가지 방법이 있습니다. 즉, IMHO는 WPF Plain TextBox 컨트롤을 사용하는 것이 좋습니다.
Vector는 제목 표시 줄 도구> 옵션> 디버깅> 일반에서 HotReload를 숨기는 방법에 대해서도 언급했습니다. XAML
확인 코드에 대한 UI 디버깅 도구 사용의 선택을 취소하십시오. 아래가 도움이되기를 바랍니다. 맞춤법 검사를 위해 WinForms에서 WPF 컨트롤을 사용하기로 결정한 경우

Public Class frmStart

Dim rtb As Windows.Controls.RichTextBox = New Windows.Controls.RichTextBox()
Dim tb As Windows.Controls.TextBox = New Windows.Controls.TextBox()
Dim str As String = " "

Private Sub frmStart_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ElementHost1.Child = rtb
    rtb.SpellCheck.IsEnabled = True

    ElementHost2.Child = tb
    tb.SpellCheck.IsEnabled = True

    If str.Length < 100 Then
        rtb.VerticalScrollBarVisibility = Windows.Controls.ScrollBarVisibility.Visible
    End If
End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    str = "Plain WPF TxtBox"
    tb.Text = str
    rtb.AppendText("Heree is som mispelled txt se if the dictioary wrks more nonsense to see the scroll bar's will this word wrapp or is that rapp")
End Sub

Private Sub btnGet_Click(sender As Object, e As EventArgs) Handles btnGet.Click
    Dim elementHost = ElementHost1
    Dim wpfRichText = CType(elementHost.Child, Windows.Controls.RichTextBox)
    Dim range As Windows.Documents.TextRange = New Windows.Documents.TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd)
    Dim allText As String = range.Text
    tbMsg.Text = allText.ToString
End Sub

Private Sub btnGTB_Click(sender As Object, e As EventArgs) Handles btnGTB.Click
    Dim elementHost = ElementHost2
    Dim text = tb.Text
    tbMsg.Text = text.ToString
End Sub
1 KyleWang Aug 20 2020 at 09:25

Winforms에서 WPF 컨트롤을 호스팅하려면 다음 두 가지 방법을 참조 할 수 있습니다.

첫째, 둘 다 ElementHost폼에 컨트롤 을 추가해야합니다 .

솔루션 A :

wpf 컨트롤을 직접 선언 (Windows.Controls 사용)

Dim rtb As Windows.Controls.RichTextBox = New Windows.Controls.RichTextBox()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    rtb.SpellCheck.IsEnabled = True
    ElementHost1.Child = rtb
End Sub

솔루션 B :

User Control(WPF)다음과 같이 "UserControl1.xaml"에서 새로 만들고 내용을 편집합니다.

<Grid>
    <RichTextBox x:Name="richtextbox" Foreground="Black" FontSize="24" Margin="0"></RichTextBox>
    <RichTextBox SpellCheck.IsEnabled="True" />
</Grid>

그런 다음 'form1.vb'의 코드를 수정합니다.

Private uc As UserControl1 = New UserControl1()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ElementHost1.Child = uc
End Sub