我用WPF编写了一个富文本编辑器,但有两个问题不能解决,望各位前辈指点!
1.textEditor.xaml前台代码如下:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
xmlns:fluent="clr-namespace:Fluent;assembly=Fluent"
xmlns:extwpftoolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
mc:Ignorable="d" d:DesignHeight="730" d:DesignWidth="1354"
HorizontalAlignment="Left" VerticalAlignment="Top">
SmallImageSource="ico/Notepad.png"
LargeImageSource="ico/Notepad.png"
ToolTipTitle="WPF4 Notepad"
ToolTipDescription="Notepad Application with Ribbon Sample" />
LabelTitle="字体"
CanExecute="FontStyleCommand_CanExecute"
Executed="FontStyleCommand_Executed"
ToolTipTitle="FontStyle"
ToolTipDescription="FontStyle selected contents"
/>
Command="{StaticResource FontColorCommand}">
CanExecute="FontColorComboBox_CanExecute"
Executed="FontColorComboBox_Executed"
ToolTipTitle="FontColor"
ToolTipDescription="FontColor selected contents" />
2.textEditor.xaml.cs 后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
///引入控件
///using Xceed.Wpf.Toolkit;
using Microsoft.Windows.Controls.Ribbon;
using Fluent;
using Microsoft.WindowsAPICodePack.Dialogs;
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.Win32;
using System.IO;
using System.Reflection;
using System.Xml.Linq;
using System.Resources;
using System.Windows.Resources;
using System.Threading;
using System.Windows.Controls.Primitives;
namespace Shanzhizi.Controls.TextEditor
{
///
/// textEditorControl.xaml 的交互逻辑
///
public partial class textEditor : UserControl
{
public textEditor()
{
InitializeComponent();
#region 加载字体颜色
fontColorComboBox();
#endregion 加载字体颜色
}
#region RibbonTab
#region RibbonTab 开始
#region RibbonTab 开始 字体
private void FontStyleCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
}
private void FontStyleCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
}
#region RibbonTab 字体 设置颜色
public void fontColorComboBox()
{
PropertyInfo[] props = typeof(Colors).GetProperties();
foreach (PropertyInfo prop in props)
{
Color clr = (Color)prop.GetValue(null, null);
bool isBlack = .222 * clr.R + .707 * clr.G + .071 * clr.B > 128;
Label label = new Label();
label.Content = prop.Name;
label.Background = new SolidColorBrush(clr);
label.Foreground = isBlack ? Brushes.Black : Brushes.White;
label.Width = 80;
label.Margin = new Thickness(0, 0, 0, 0);
label.Tag = clr;
// CanvasBackgroundListBoxColor.Items.Add(label);
fontcolorcombobox.Items.Add(label);
}
}
private void FontColorComboBox_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (richtextbox != null && richtextbox.Selection.Text.Length > 0)
{
e.CanExecute = true;
}
}
private void FontColorComboBox_Executed(object sender, ExecutedRoutedEventArgs e)
{
if (richtextbox != null && richtextbox.Selection.Text.Length > 0)
{
// var label = fontcolorcombobox.SelectedItem as Label;
// if (label != null)
// {
// var fontcolor = label.Background as SolidColorBrush;
//richtextbox.Selection.ApplyPropertyValue(Run.ForegroundProperty, (fontcolorcombobox.SelectedItem
as Label).Background.ToString());
//}
}
}
#endregion RibbonTab 字体 设置颜色
#endregion RibbonTab 字体
#endregion RibbonTab
}
}
以上是我编写的代码,为方便开我将其他无关代码删除:
第一个问题:加下划线的代码,该RibbonComboBox容器是用来放置字体的容器,RibbonComboBoxItem可以放置各种文本字体,但是当有很多字体时,前台代码显得比价笨重,却相当繁琐。现在在该工程目录下有一个Fonts的文字字体文件包,其中放置上百个文字字体文件,现在我想通过后台代码将该字体Fonts文件中的字体文件自动加载到前台RibbonComboBox容器中,形成RibbonComboBoxItem子项,并显示在RibbonComboBox中,当选择RibbonComboBoxItem的字体样式时,通过后台代码可将选中的文字变为相应的字体样式。
要实现这样的功能,后台代码该如何补写?希望前辈尽可能在现有基础上补写代码。
第二个问题:黑体代码。该RibbonComboBox容器是用来放置字体颜色的容器,我已经通过后台代码在该RibbonComboBox容器放入了很多颜色,代码实现如上加黑及下划线部分,并已成功宣示。当我选择该RibbonComboBox容器的颜色是,通过后台代码可将所选的文字颜色进行相应的改变。但是当我选择颜色后程序无法运行。
我要实现文本字体颜色的变化我该如何改写代码的功能实现部分(该部分已被注解),或是该代码有问题,如果有问题我应该怎样做,希望前辈尽可能在现有基础上补写代码或修改代码。