繁体   English   中英

使用包装面板模板在项目控件中强制中断

[英]Force a break in a items control with wrap panel template

我一直想解决一个有趣的问题。 基本上,我有一个WrapPanel控件,它使用WrapPanel作为ItemsPanel来模拟由多个绑定字符串构建的段落。 但是,有时候我需要强制中断,例如当我开始一个新段落时,但是在TextBlock DateTemplate中插入一个中断实际上并没有在父包装面板中插入一个中断。 这是代码:

<ItemsControl ItemsSource="{Binding Fragments}" >
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock
                TextWrapping="Wrap"
                Text="{Binding}"/> <!--If this text has a break it won't 
                                        propagate that break to the wrap panel,
                                        but instead just in this text block which
                                        causes the formatting to look wrong-->
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

这是片段的简单定义,将显示我在说什么:

Fragments = new ObservableCollection<string>();
Fragments.Add("This is");
Fragments.Add("the first line, ");
Fragments.Add("it is very long and will drift to the ");
Fragments.Add("second line naturally since it is controlled by a wrap panel");
Fragments.Add("\n\r This I want to force to the line below where the line above ends");
Fragments.Add("rapid \n\r new \n\r lines");

在此处输入图片说明

我希望这些段落可以继续串联在一起,但是请您在遇到手动中断时遵守它们。 像这样:

This is the first line, it is very long and will drift to the second line 
naturally since it is controlled by a wrap panel.
This I want to force to the line below where the line above ends.
rapid
new
lines

我会抛弃ItemsControl并使用文本块的Inlines集合代替。 不幸的是,您不能直接绑定字符串集合,因为TextBlock.Inlines不是依赖项属性,但是使用附加的依赖项属性来解决它并不难:

我还添加了对CollectionChanged事件的传播的支持,因此向ViewModel.Fragments添加一个字符串将更新文本块。 删除也将起作用,尽管限制是将删除与字符串匹配的第一个Fragment。

在此处输入图片说明

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">

    <Window.DataContext>
        <local:ViewModel />
    </Window.DataContext>

    <Grid>
        <TextBlock local:FlowSupport.Fragments="{Binding Fragments}" TextWrapping="WrapWithOverflow" Margin="10" Background="Beige" />
    </Grid>
</Window>

视图模型:

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    private ObservableCollection<string> _fragments;
    public ObservableCollection<string> Fragments { get { return _fragments; } set { _fragments = value; OnPropertyChanged("Fragments"); } }

    public ViewModel()
    {
        Fragments = new ObservableCollection<string>();
        Fragments.Add("This is ");
        Fragments.Add("the first line, ");
        Fragments.Add("it is very long and will drift to the ");
        Fragments.Add("second line naturally since it is controlled by a wrap panel");
        Fragments.Add("\nThis I want to force to the line below where the line above ends\n");
        Fragments.Add("rapid \nnew \nlines");
    }
}

FlowSupport:

using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Collections.Specialized;

namespace WpfApplication1
{
    public static class FlowSupport
    {
        private static Dictionary<TextBlock, NotifyCollectionChangedEventHandler> _collChangedHandlers = new Dictionary<TextBlock,NotifyCollectionChangedEventHandler>();

        public static ObservableCollection<string> GetFragments(TextBlock tb) { return (ObservableCollection<string>)tb.GetValue(FragmentsProperty); }
        public static void SetFragments(TextBlock tb, ObservableCollection<string> value) { tb.SetValue(FragmentsProperty, value); }

        public static readonly DependencyProperty FragmentsProperty = DependencyProperty.RegisterAttached("Fragments", typeof(ObservableCollection<string>), typeof(FlowSupport), new PropertyMetadata(new ObservableCollection<string>(), OnFragmentsChanged));

        private static void OnFragmentsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var tb = d as TextBlock;
            if (tb != null)
            {
                CreateCollectionChangedHandler(tb); // create handler, once per textblock

                tb.Inlines.Clear();
                var oldInlines = e.OldValue as ObservableCollection<string>;
                if (oldInlines != null)
                {
                    oldInlines.CollectionChanged -= _collChangedHandlers[tb];
                }
                var inlines = e.NewValue as ObservableCollection<string>;
                if (inlines != null)
                {
                    inlines.CollectionChanged += _collChangedHandlers[tb];

                    foreach (string s in inlines)
                        tb.Inlines.Add(s);
                }
            }
        }

        private static void CreateCollectionChangedHandler(TextBlock tb)
        {
            if (!_collChangedHandlers.ContainsKey(tb))
            {
                _collChangedHandlers.Add(tb, (s1, e1) =>
                {
                    if (e1.NewItems != null)
                    {
                        foreach (string text in e1.NewItems)
                            tb.Inlines.Add(text);
                    }
                    if (e1.OldItems != null)
                    {
                        foreach (string text in e1.OldItems)
                        {
                            Inline inline = tb.Inlines.FirstOrDefault(i => ((Run)i).Text == text);
                            if (inline != null)
                                tb.Inlines.Remove(inline);
                        }
                    }
                });
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM