wpf richtextbox ในแอปพลิเคชัน winforms
ฉันต้องการใช้ WPF RichTextBox ในโครงการ WinForms ที่เขียนด้วย VB
ฉันได้สร้างโครงการ WinForms ด้วยรูปแบบเดียวและปุ่ม
จากนั้นฉันก็เพิ่มไลบรารีการควบคุมผู้ใช้ WPF โครงการใหม่วาง WPF RichTextBox บนแบบฟอร์ม WPF
ฉันเพิ่มการทำงานร่วมกันของ ElementHost ให้กับ WinForm พร้อมการนำเข้าเหล่านี้
Imports System
Imports System.Windows.Forms
Imports System.Windows.Forms.Integration
จากที่นี่ฉันหลงทางบางส่วนของคำถาม SO อายุ 10 ถึง 7 ปีบทช่วยสอน MS ไม่ใช่
รหัสช่วยเหลือจาก WPF Form มากนัก
Public Class UserControl1
ReadOnly rtbWPF As New UserControl
ElementHost
wpfwindow.Show
End Class
ฉันไม่ได้โพสต์รหัส XAML ไม่แน่ใจว่าต้องทำอย่างไร
ดังนั้นคำถามจะทำอย่างไรต่อไปเพื่อเชื่อมโยงแบบฟอร์ม WPF กับ RTB กับแบบฟอร์ม WinForms?
ฉันต้องการโหลดข้อมูลจาก SQLite DB ลงใน WPF RichTextBox และบันทึกข้อความที่ป้อนใน RTB ลงใน DB
คำตอบ
คำตอบนี้มีไว้เพื่อขยายในคำตอบที่ยอดเยี่ยมของ @KyleWang
หนึ่งปัญหาใหญ่กับตัวเลือกเวกเตอร์ของ WPF RichTextBox คือไม่มีคุณสมบัติข้อความในการควบคุม WPF RichTextBox นี่คือวิธีหนึ่งในการดึงข้อความทั้งหมดออกมา ที่กล่าวว่าฉันจะให้ IMHO แนะนำให้ใช้ WPF Plain TextBox control
Vector ยังแสดงความคิดเห็นเกี่ยวกับวิธีซ่อน HotReload ในแถบหัวเรื่องเครื่องมือ> ตัวเลือก> การดีบัก> ทั่วไป> ยกเลิกการตรวจสอบเปิดใช้งานเครื่องมือดีบัก UI สำหรับ
รหัสXAML OK ด้านล่างหวังว่านี่จะเป็นประโยชน์ หากคุณตัดสินใจที่จะใช้ตัวควบคุม WPF ใน WinForms สำหรับการตรวจสอบการสะกด
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
ในการโฮสต์คอนโทรล WPF ใน Winforms คุณสามารถอ้างถึงสองวิธีต่อไปนี้
ขั้นแรกทั้งคู่ต้องเพิ่มตัว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