我正在尝试从 TextBox 的上下文绑定到 WebBrowser 组件。
即使调试成功,XamlDesign 也会返回 InvalidCastException 错误。有人可以帮我解决这个问题吗?
我的WPF项目基于示例项目(https://www.codeproject.com/Articles/1097390/Displaying-HTML-in-a-WPF-RichTextBox),下载的项目也返回相同的错误。
我的WPF项目中的代码如下:
主窗口.xaml
<Window x:Class="Wpf_HTML_display.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Wpf_HTML_display"
mc:Ignorable="d"
Height="1176" Width="1920" WindowState="Maximized">
<Canvas>
<TextBox x:Name="srcHTML" Height="292" Width="1753" AcceptsReturn="True"><p></p></TextBox>
<WebBrowser x:Name="WebBrowser1" local:WebBrowserBehavior.Body="{Binding ElementName=srcHTML, Path=Text}" Height="838" Width="1743" Canvas.Left="10" Canvas.Top="297" />
</Canvas>
</Window>
WebBrowserBehavior 类
public class WebBrowserBehavior
{
public static readonly DependencyProperty BodyProperty =
DependencyProperty.RegisterAttached("Body", typeof(string), typeof(WebBrowserBehavior),
new PropertyMetadata(OnChanged));
public static string GetBody(DependencyObject dependencyObject)
{
return (string)dependencyObject.GetValue(BodyProperty);
}
public static void SetBody(DependencyObject dependencyObject, string body)
{
dependencyObject.SetValue(BodyProperty, body);
}
private static void OnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) =>
((WebBrowser)d).NavigateToString((string)e.NewValue);
}
XamlDesign 返回:
Unable to cast object of type 'Microsoft.VisualStudio.DesignTools.WpfDesigner.InstanceBuilders.HwndHostInstance' to type 'System.Windows.Controls.WebBrowser'.
收到一只叮咚
相关分类