1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| /// <summary>
/// Permet d'effectuer une liaison de données sur la propriété Blocks d'un FlowDocument
/// </summary>
public static class FlowDocumentBehavior
{
/// <summary>
/// Obtient la valeur de la propriété attachée BindableBlocks pour l'objet spécifié
/// </summary>
/// <param name="obj">Objet dont on souhaite obtenir la valeur de la propriété</param>
/// <returns></returns>
public static IEnumerable<Block> GetBindableBlocks(DependencyObject obj)
{
return (IEnumerable<Block>)obj.GetValue(BindableBlocksProperty);
}
/// <summary>
/// Définit la valeur de la propriété attachée BindableBlocks pour l'objet spécifié
/// </summary>
/// <param name="obj">Objet dont on souhaite définir la valeur de la propriété</param>
/// <param name="value">Valeur de la propriété de dépendance spécifiée</param>
public static void SetBindableBlocks(DependencyObject obj, IEnumerable<Block> value)
{
obj.SetValue(BindableBlocksProperty, value);
}
/// <summary>
/// Propriété attachée BindableBlocks
/// </summary>
public static readonly DependencyProperty BindableBlocksProperty =
DependencyProperty.RegisterAttached("BindableBlocks",
typeof(IEnumerable<Block>), typeof(FlowDocumentBehavior), new UIPropertyMetadata(null, BindableBlocksChanged));
private static void BindableBlocksChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
var flowDocument = dependencyObject as FlowDocument;
if (flowDocument == null) return;
// Désabonnement de l'ancienne collection
if (e.OldValue != null)
{
var sync = GetFlowDocumentSync(flowDocument);
if (sync != null)
{
sync.Dispose();
SetFlowDocumentSync(flowDocument, null);
}
}
if (e.NewValue != null)
{
var sync = new FlowDocumentSync(flowDocument, (IEnumerable<Block>)e.NewValue);
SetFlowDocumentSync(flowDocument, sync);
}
}
private static FlowDocumentSync GetFlowDocumentSync(DependencyObject obj)
{
return (FlowDocumentSync)obj.GetValue(_flowDocumentSyncPropertyKey.DependencyProperty);
}
private static void SetFlowDocumentSync(DependencyObject obj, FlowDocumentSync value)
{
obj.SetValue(_flowDocumentSyncPropertyKey, value);
}
private static readonly DependencyPropertyKey _flowDocumentSyncPropertyKey =
DependencyProperty.RegisterAttachedReadOnly("flowDocumentSync",
typeof(FlowDocumentSync), typeof(FlowDocumentBehavior), new UIPropertyMetadata(null));
}
internal class FlowDocumentSync : IDisposable
{
private FlowDocument _flowDocument;
private IEnumerable<Block> _blocks;
public FlowDocumentSync(FlowDocument flowDocument, IEnumerable<Block> blocks)
{
_flowDocument = flowDocument;
_blocks = blocks;
_flowDocument.Blocks.Clear();
foreach (var block in _blocks)
{
_flowDocument.Blocks.Add(block);
}
var observableList = _blocks as INotifyCollectionChanged;
if (observableList != null) observableList.CollectionChanged += BlocksCollectionChanged;
}
private void BlocksCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.OldItems != null)
{
foreach (Block item in e.OldItems)
{
_flowDocument.Blocks.Remove(item);
}
}
if (e.NewItems != null)
{
foreach (Block item in e.NewItems)
{
_flowDocument.Blocks.Add(item);
}
}
}
public void Dispose()
{
var observableList = _blocks as INotifyCollectionChanged;
if (observableList != null) observableList.CollectionChanged -= BlocksCollectionChanged;
}
} |
Partager