博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF:Documents文档--Annomation批注(3)
阅读量:6403 次
发布时间:2019-06-23

本文共 5347 字,大约阅读时间需要 17 分钟。

AnnotationStyling批注样式

clipboard.png

1、实现效果

  1. 可以选择不同便笺样式,对所有批注应用样式
  2. 选择文本后实现对批注的文本、墨迹便笺创建、删除
  3. 对选择文本的部分创建高亮不同颜色的批注,及实现清除命令

2、关注词

  1. StickyNoteControl+自定义样式类

3、静态组织

流文档页面浏览器的资源字典设定,可作为ComboBox的绑定源参数

ComboBox的绑定设置:

  1. ItemsSource绑定源为文档的资源字典,通过转换获取Collection<StyleMetaData>数据项
  2. 子项模板绑定为上述数据项的其Key值来显示
  3. 新建高亮批注按钮绑定到批注服务AnnotationService的路由命令字段。同时传递不同颜色的命令参数CommandParameter
.....

ResourceConverter资源字典转换为自定义Collection<StyleMetaData>:

  1. 获取选定项的类型Collection<ResourceDictionary>及新建要返回的类型Collection<StyleMetaData>
  2. 找到字典中对应的类型StickyNoteControl(Type.TargetType.Equals())
  3. 循环复制键值对到自定义类StyleMetaData
[ValueConversion(typeof (Collection
), typeof (Collection
))]public class ResourceEntryToComboItemConverter : IValueConverter{ // ----------------------------- Convert ------------------------------ ///
/// Parses a collection of ResourceDictionaries and /// extracts all StickyNote styles. /// ///
/// A list of Styles and their associated /// keys (for identification). ///
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var unfiltered = value as Collection
; var filtered = new Collection
(); foreach (var dict in unfiltered) { foreach (string key in dict.Keys) { var aStyle = dict[key] as Style; if (aStyle != null && aStyle.TargetType.Equals(typeof (StickyNoteControl))) filtered.Add(new StyleMetaData(key, aStyle)); } } return filtered; } // end:Convert // --------------------------- ConvertBack ---------------------------- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => null;}

自定义类StyleMetaData:

此类需要继承依赖性,注册Key依赖属性,方便ComboBox子项数据模板绑定Text="{binding Path=Key}"

/// ///     Provides a simple wrapper that associates a///     Style with its ResourceDictionary key./// public class StyleMetaData : DependencyObject{    public static DependencyProperty KeyProperty =        DependencyProperty.Register("Key", typeof (string), typeof (StyleMetaData));    public Style Value;    public StyleMetaData(string key, Style value)    {        Key = key;        Value = value;    }    public string Key    {        get { return (string) GetValue(KeyProperty); }        set { SetValue(KeyProperty, value); }    }}

4、运行流程

当选择ComboBox不同选择项时:

  1. 获取选定项及其对应样式实例new Style(),继承此样式即可得新样式
  2. 清除文档的资源,添加新建的样式资源
  3. 重新加载批注,因此批注会自动应用新便笺样式
// ------------------------- OnStyleSelected --------------------------/// ///     Replaces the default StickyNote style when a new///     style is selected from the drop down combo box./// protected void OnStyleSelected(object sender, SelectionChangedEventArgs e){    // Extract the selected style.    var source = (ComboBox) e.Source;    var selectedStyle = (StyleMetaData) source.SelectedItem;    var newStyle = new Style(typeof (StickyNoteControl)) {BasedOn = selectedStyle.Value};    // Replace the default StickyNote style with the one that was just selected.    var defaultKey = typeof (StickyNoteControl);    if (Viewer.Resources.Contains(defaultKey))        Viewer.Resources.Remove(defaultKey);    Viewer.Resources.Add(defaultKey, newStyle);    // Re-load annotations so that they pickup new style.    var service = AnnotationService.GetService(Viewer);    service.Disable();    service.Enable(service.Store);}

主窗口加载时:获取存储的批注便笺并加载

/// ///     Turns Annotations on./// protected void OnLoaded(object sender, RoutedEventArgs e){    // Make sure that an AnnotationService isn�t already enabled.    var service = AnnotationService.GetService(Viewer);    if (service == null)    {        // (a) Create a Stream for the annotations to be stored in.        _annotationStream =            new FileStream("annotations.xml", FileMode.OpenOrCreate);        // (b) Create an AnnotationService on our        // FlowDocumentPageViewer.        service = new AnnotationService(Viewer);        // (c) Create an AnnotationStore and give it the stream we        // created. (Autoflush == false)        AnnotationStore store = new XmlStreamStore(_annotationStream);        // (d) "Turn on annotations". Annotations will be persisted in        // the stream created at (a).        service.Enable(store);    }}

关闭主窗口时:保存批注编辑信息,把缓冲区数据写入xmlw文件存储

/// ///     Turns Annotations off./// protected void OnUnloaded(object sender, RoutedEventArgs e){    // (a) Check that an AnnotationService    // actually existed and was Enabled.    var service = AnnotationService.GetService(Viewer);    if (service != null && service.IsEnabled)    {        // (b) Flush changes to annotations to our stream.        service.Store.Flush();        // (c) Turn off annotations.        service.Disable();        // (d) Close our stream.        _annotationStream.Close();    }}

最后样式便笺样式xaml:

StylingUsingProperties.xaml:

AdvancedStyling.xaml:

DefaultStickeyNoteStyle.xaml:

40.0
30.0

转载地址:http://ulnea.baihongyu.com/

你可能感兴趣的文章
JavaScript面向对象轻松入门之多态(demo by ES5、ES6、TypeScript)
查看>>
【数据结构】线性表(一):顺序列表
查看>>
利用Mallet工具自动挖掘文本Topic
查看>>
Windows下oracle打补丁步骤
查看>>
Python教程(一)Python简介
查看>>
asp.net forms认证
查看>>
一帧图像的两种显示器建模方式
查看>>
Hadoop 公平调度器算法调度解析
查看>>
Linux Foundation(笔记)
查看>>
Java学习第二十五天
查看>>
vim配置
查看>>
ubuntu 把软件源修改为国内源和更新
查看>>
随机产生四则运算,导入导出文件
查看>>
位运算符
查看>>
winform自定义控件
查看>>
C#编码好习惯
查看>>
避其锋芒,侧翼出击。——司马亮创业回忆录(一)
查看>>
scope
查看>>
一起谈.NET技术,晚绑定场景下对象属性赋值和取值可以不需要PropertyInfo
查看>>
一起谈.NET技术,.Net Framework源代码中的模式之Prototype(原型模式)
查看>>