私の目標はこの質問に似て
いますが、それでも私の問題に対する答えは得られませんでした
そのため、レポート テンプレートを設計および生成するための WYSIWYG HTML 編集サポートを備えたアプリを作成する必要があります。上記の質問のように、WPF で WebBrowser コントロールを使用しました。最大の問題は、designMode が on に設定された後、WPF WebBrowser が常に null を HTML 本文に設定することです。そのため、WinForm WebBrowser をアプリにホストします。また、HTML ドキュメントを WebBrowser から処理するように設定または取得するのは非常に困難です。
Q:
- WinForm を変更せずに WPF を使用してこれ (WebBrowser コントロールを使用する HTML エディター) を実現する方法はありますか?
- または、そうでない場合。WYSIWYG HTML Editor を Visual Editor で作成するための回避策、記事、コード、またはその他のものはありますか?
更新:
MVVM の目的で、これら 2 つの添付プロパティがあります。そのため、HTMLSource を使用して HTML を取得/設定し、アプリの起動時にデザイン モードを設定できます。
IsDesignMode
public static readonly DependencyProperty IsDesignModeProperty =
DependencyProperty.RegisterAttached("IsDesignMode", typeof (Boolean), typeof (WebBrowserHelper),
new UIPropertyMetadata(false, IsDesignModePropertyChanged));
public static Boolean GetIsDesignMode(DependencyObject obj)
{
return (Boolean)obj.GetValue(IsDesignModeProperty);
}
public static void SetIsDesignMode(DependencyObject obj, Boolean value)
{
obj.SetValue(IsDesignModeProperty, value);
}
public static void IsDesignModePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
WebBrowser browser = obj as WebBrowser;
if (browser != null)
{
Boolean designMode = (Boolean) args.NewValue;
if(designMode)
{
browser.LoadCompleted += (s, e) =>
{
var htmlDoc = (s as WebBrowser).Document as IHTMLDocument2;
htmlDoc.body.setAttribute("contenteditable", "true");
htmlDoc.designMode = "On";
};
}
else
{
browser.LoadCompleted += (s, e) =>
{
var htmlDoc = (s as WebBrowser).Document as IHTMLDocument2;
htmlDoc.body.setAttribute("contenteditable", "false");
htmlDoc.designMode = "Off";
};
}
}
}
HTMLソース
public static readonly DependencyProperty HTMLSourceProperty =
DependencyProperty.RegisterAttached("HTMLSource", typeof (String), typeof (WebBrowserHelper),
new UIPropertyMetadata(null, HTMLSourcePropertyChanged));
public static String GetHTMLSource(DependencyObject obj)
{
return (String)obj.GetValue(HTMLSourceProperty);
}
public static void SetHTMLSource(DependencyObject obj, String value)
{
obj.SetValue(HTMLSourceProperty, value);
}
public static void HTMLSourcePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs args)
{
WebBrowser browser = o as WebBrowser;
if (browser != null)
{
browser.NavigateToString(args.NewValue as String);
}
}
見る
<UserControl x:Class="Delay.View.LayoutView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:browser="clr-namespace:Delay.Helper"
xmlns:cal="http://www.caliburnproject.org"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
<UserControl.Resources>
<Style x:Key="ButtonStyle" TargetType="Button">
<Setter Property="Width" Value="100" />
<Setter Property="Height" Value="40" />
<Setter Property="Margin" Value="2.5,0" />
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel>
<ContentPresenter Content="{TemplateBinding Content}"
TextBlock.FontSize="15" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid Background="Lavender">
<DockPanel>
<TextBlock HorizontalAlignment="Center" Text="Layout Designer"
DockPanel.Dock="Top" FontSize="20" />
<WebBrowser Name="webBrowser" HorizontalAlignment="Stretch" DockPanel.Dock="Top" Margin="8" Height="435"
browser:WebBrowserHelper.HTMLSource="{Binding HtmlPage}" browser:WebBrowserHelper.IsDesignMode="True" />
<StackPanel Orientation="Horizontal" DockPanel.Dock="Bottom" HorizontalAlignment="Center">
<StackPanel Orientation="Vertical" Margin="5,0">
<ComboBox ItemsSource="{Binding LayoutTags}" SelectedItem="{Binding SelectedTag}"
HorizontalAlignment="Stretch" Margin="0,5" MinWidth="100">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock FontSize="12" Text="{Binding TagName}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<ListBox ItemsSource="{Binding LayoutValueTypes}" SelectedItem="{Binding SelectedType}"
Width="{Binding ElementName=cmbTag, Path=ActualWidth}" Height="70">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock FontSize="12" Text="{Binding TypeName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<CheckBox Name="IsDesignMode" Content="Design Mode" TextBlock.FontSize="12">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<cal:ActionMessage MethodName="DesignModeOnOff">
<cal:Parameter Value="{Binding ElementName=webBrowser}" />
<cal:Parameter Value="{Binding IsDesignMode}" />
</cal:ActionMessage>
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
</StackPanel>
<Button Name="PutComponent" Style="{StaticResource ButtonStyle}"
Content="Put" />
<Button Name="SaveLayout" Style="{StaticResource ButtonStyle}"
Content="Save" />
</StackPanel>
</StackPanel>
</DockPanel>
</Grid>
</UserControl>