将ObservableCollection <block>绑定到存储在FlowDocument段落中的ItemsControl的正确方法是什么?</block>

时间:2014-06-01 06:36:41

标签: wpf richtextbox

我尝试将ObservableCollection绑定到存储在FlowDocument段落中的ItemsControl,但块没有按预期设计内容。

我有自动控制包裹控件,如下所示:

public class HelpCtrl : Control
{
    static HelpCtrl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(HelpCtrl), new FrameworkPropertyMetadata(typeof(HelpCtrl)));
    }

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(HelpCtrl), new UIPropertyMetadata(null, OnTextChanged));

    static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        MemoryStream stream = new MemoryStream(File.ReadAllBytes(e.NewValue.ToString()));
        RichTextBox r = new RichTextBox();
        r.Selection.Load(stream, DataFormats.Rtf);
        HelpCtrl helpCtrl = d as HelpCtrl;

        ObservableCollection<Block> l = new ObservableCollection<Block>();
        while (r.Document.Blocks.Count > 0)
        {
            var block = r.Document.Blocks.FirstBlock;
            r.Document.Blocks.Remove(block);
            l.Add(block);
        }
        helpCtrl.Blocks = l;
    }



    public ObservableCollection<Block> Blocks
    {
        get { return (ObservableCollection<Block>)GetValue(BlocksProperty); }
        set { SetValue(BlocksProperty, value); }
    }

    public static readonly DependencyProperty BlocksProperty =
        DependencyProperty.Register("Blocks", typeof(ObservableCollection<Block>), typeof(HelpCtrl), new UIPropertyMetadata(null));


}

<Style TargetType="{x:Type local:HelpCtrl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:HelpCtrl}">
                <Grid>
                    <Grid x:Name="gridRichTextBox" Background="Black" Width="350" Height="600">
                        <RichTextBox   
                            IsReadOnly="True"
                            x:Name="rtbHelpText"            
                            VerticalScrollBarVisibility="Auto" 
                            HorizontalScrollBarVisibility="Auto"
                            >
                            <FlowDocument>
                                <Paragraph>
                                    <ItemsControl ItemsSource="{Binding Blocks, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:HelpCtrl}}}"/>

                                </Paragraph>
                            </FlowDocument>
                        </RichTextBox>
                    </Grid>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我从rtf文件加载内容并设计它,但我看到绑定不好,结果:

“System.Windows.Documents.Paragraph”。

如何解决?

0 个答案:

没有答案