none
Set FlowDocument Style in code RRS feed

  • Question

  • I'm trying to set FlowDocument Block.Margin property in code (so when edited in wpf RichTextBox there is not a gap between paragraphs). This can be done in Xaml using;

    <FlowDocument>
      <FlowDocument.Resources>
        <Style TargetType="{x:Type Paragraph}">
          <Setter Property="Margin" Value="0"/>
        </Style>
      </FlowDocument.Resources>
      <Paragraph></Paragraph> 
    </FlowDocument>
    

    This can be represented as the following Xaml string;

    <FlowDocument PagePadding=\"5,0,5,0\" AllowDrop=\"False\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"><FlowDocument.Resources><Style TargetType=\"Paragraph\" x:Key=\"{x:Type Paragraph}\"><Style.Resources><ResourceDictionary /></Style.Resources><Setter Property=\"Block.Margin\"><Setter.Value><Thickness>0,0,0,0</Thickness></Setter.Value></Setter></Style></FlowDocument.Resources><Paragraph></Paragraph></FlowDocument>


    So how to achieve this in code? My attempt was;

    Style style = new Style(typeof(Paragraph));
    style.Setters.Add(new Setter(Block.MarginProperty, new Thickness(0)));
    flowDocument.Resources.Add("MarginStyle", style);


    But margin doesn't change in the FlowDocument. I also tried;

    flowDocument.SetResourceReference(Block.MarginProperty, new Thickness(0)); 
    
    Tuesday, July 7, 2009 9:46 AM

Answers

  • Hi,

     

    The approach you tried did not work because you specify a explicit style key for the Paragraph. But you did not reference that key in your Paragraph instance. So the style will not be automatically applied to the Paragraph. You can try to set the implicit style like this:

    Code snippet:

      Style style = new Style(typeof(Paragraph));

      style.Setters.Add(new Setter(Block.MarginProperty, new Thickness(100)));

      flowDocument.Resources.Add(typeof(Paragraph), style);

     

    Here, we utilize the typeof syntax to retrieve the default style key. Which in XAML is set like this:

       <Style x:Key="{x:Type Paragraph}" TargetType="{x:Type Paragraph}">

           <Setter Property="Margin" Value="100"/>

       </Style>

     

    Of course, if you really want to use the explicit style key for FlowDocument, you need to add reference for Paragraph:
    XAML code:

      <FlowDocument Name="flowDocument">

                    <Paragraph Style="{DynamicResource MarginStyle}">aaaaaaa</Paragraph>

                    <Paragraph  Style="{DynamicResource MarginStyle}">bbbb</Paragraph>

                    <Paragraph  Style="{DynamicResource MarginStyle}">cccc</Paragraph>

     </FlowDocument>

     

    In the code behind:

    public Window1()

            {

                InitializeComponent();

                Style style = new Style(typeof(Paragraph));

                style.Setters.Add(new Setter(Block.MarginProperty, new Thickness(100)));

                flowDocument.Resources.Add("MarginStyle", style);

            }

    You may notice that I am using the dynamic resource syntax instead of using static resource, because in this case we need to use the forward reference go apply the style, but static resource does not support forward reference.

     

    For your information, you can refer this blog article on how to create style and template in code:

    http://www.codeproject.com/KB/WPF/codeVsXAML.aspx

     

    If you are still having additional issues with this, please feel free to ask.

    Thanks.

    Monday, July 13, 2009 9:42 AM

All replies

  • Hi,

     

    The approach you tried did not work because you specify a explicit style key for the Paragraph. But you did not reference that key in your Paragraph instance. So the style will not be automatically applied to the Paragraph. You can try to set the implicit style like this:

    Code snippet:

      Style style = new Style(typeof(Paragraph));

      style.Setters.Add(new Setter(Block.MarginProperty, new Thickness(100)));

      flowDocument.Resources.Add(typeof(Paragraph), style);

     

    Here, we utilize the typeof syntax to retrieve the default style key. Which in XAML is set like this:

       <Style x:Key="{x:Type Paragraph}" TargetType="{x:Type Paragraph}">

           <Setter Property="Margin" Value="100"/>

       </Style>

     

    Of course, if you really want to use the explicit style key for FlowDocument, you need to add reference for Paragraph:
    XAML code:

      <FlowDocument Name="flowDocument">

                    <Paragraph Style="{DynamicResource MarginStyle}">aaaaaaa</Paragraph>

                    <Paragraph  Style="{DynamicResource MarginStyle}">bbbb</Paragraph>

                    <Paragraph  Style="{DynamicResource MarginStyle}">cccc</Paragraph>

     </FlowDocument>

     

    In the code behind:

    public Window1()

            {

                InitializeComponent();

                Style style = new Style(typeof(Paragraph));

                style.Setters.Add(new Setter(Block.MarginProperty, new Thickness(100)));

                flowDocument.Resources.Add("MarginStyle", style);

            }

    You may notice that I am using the dynamic resource syntax instead of using static resource, because in this case we need to use the forward reference go apply the style, but static resource does not support forward reference.

     

    For your information, you can refer this blog article on how to create style and template in code:

    http://www.codeproject.com/KB/WPF/codeVsXAML.aspx

     

    If you are still having additional issues with this, please feel free to ask.

    Thanks.

    Monday, July 13, 2009 9:42 AM
  • Thanks. That works correctly.
    Tuesday, July 14, 2009 12:24 PM