Propriété attachée - FlowDocument WPF
Salut,
Je propose une nouvelle classe à ajouter (je l'espère :)) au projet dvp.net :
Elle permet, via une propriété attachée, de binder la propriété Blocks d'un FlowDocument à une collection (List<Block> par exemple).
Code:
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
| /// <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)
{
if (dependencyObject is FlowDocument)
{
var obj = dependencyObject as FlowDocument;
obj.Blocks.Clear();
if (e.NewValue != null)
{
foreach (var block in (IEnumerable<Block>)e.NewValue)
{
obj.Blocks.Add(block);
}
}
}
}
} |