开发者

WPF: LineBreak enable/disable dynamically

i would like to make the LineBreak element inside of that TextBlock controllable by the user in preferences to Enable/Disable it being there

<TextBlock Style="{StaticResource TextBlockStyle}" Width="130">
<TextBlock.Inline开发者_如何转开发s>
    <Run Text="{Binding Path=Name}" FontWeight="Bold" Foreground="#FF2A4D9E" />
    <Run Text="{Binding Path=Price}" FontWeight="Bold" />

       <LineBreak  />

    <Run Text="{Binding Path=Quantity}" Foreground="#99000000" />
</TextBlock.Inlines>
</TextBlock>


I don't believe there is any way in a FlowDocument to make a LineBreak not really break except to take it out. Your choices are to switch to using WPF layout or to use an attached property to switch between a LineBreak and an empty Run.

Using WPF layout

You may consider using WPF layout instead. Something like this:

<DataTemplate x:Key="Layout1">
  <DockPanel>
    <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="#FF2A4D9E" />
    <TextBlock Text="{Binding Price}" FontWeight="Bold" />
    <TextBlock Text="{Binding Quantity}" Foreground="#99000000" />
  </DockPanel>
</DataTemplate>

<DataTemplate x:Key="Layout2">
  <DockPanel>
    <DockPanel DockPanel.Dock="Top">
      <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="#FF2A4D9E" />
      <TextBlock Text="{Binding Price}" FontWeight="Bold" />
    </DockPanel>

    <TextBlock Text="{Binding Quantity}" Foreground="#99000000" />
  </DockPanel>
</DataTemplate>

Now you can easily switch between layouts just by switching DataTemplates.

Automatically removing LineBreaks using bindings

If you want to "hide" the LineBreak via a binding you can do it with an attached "BecomeLineBreak" property that, when applied to an empty Run and set true, removes it and replaces it with a LineBreak.

Like magic you now have the ability to write:

<Run my:LineBreakSwitcher.BecomeLineBreak="{Binding SomeCondition}" />

And your Run will turn into a LineBreak any time the SomeCondition property is true.

Here is the code:

public class LineBreakSwitcher : DependencyObject
{
  public static bool GetBecomeLineBreak(DependencyObject obj) { return (bool)obj.GetValue(BecomeLineBreakProperty); }
  public static void SetBecomeLineBreak(DependencyObject obj, bool value) { obj.SetValue(BecomeLineBreakProperty, value); }
  public static readonly DependencyProperty BecomeLineBreakProperty = DependencyProperty.RegisterAttached("BecomeLineBreak", typeof(bool), typeof(LineBreakSwitcher), new PropertyMetadata
  {
    PropertyChangedCallback = (obj, e) =>
      {
        var oldElement = (Inline)obj;
        var newElement = (bool)e.NewValue ? (Inline)new LineBreak() : new Run();
        newElement.SetBinding(BecomeLineBreakProperty, oldElement.GetBindingExpression(BecomeLineBreakProperty).ParentBindingBase);

        var parent = (Paragraph)oldElement.Parent;
        parent.Inlines.InsertBefore(oldElement, newElement);
        parent.Inlines.Remove(oldElement);
      }
  });

How it works: When BecomeLineBreak becomes true on a Run, a new LineBreak is created, the BecomeLineBreak binding is copied across, the LineBreak is inserted before the Run, then the Run is removed. When BecomeLineBreak become false, a new Run is created and the whole process happens in reverse.


This is what you want (100% XAML):

<TextBlock Text="{Binding Text, ElementName=MyContainer}" 
            FontWeight="Bold" FontSize="14" Name="TextBlockA" />
<TextBlock Name="TextBlockB">
    <TextBlock.Style>
        <Style>
            <Setter Property="TextBlock.Visibility" Value="Visible"/> 
            <Style.Triggers> 
                <DataTrigger Binding="{Binding Path=Text.Length, 
                        ElementName=TextBlockC}" Value="0"> 
                    <Setter Property="TextBlock.Visibility" Value="Collapsed"/> 
                </DataTrigger> 
            </Style.Triggers> 
        </Style>
    </TextBlock.Style>
    <LineBreak />
</TextBlock>
<TextBlock Text="{Binding SubText, ElementName=MyContainer}" 
            FontWeight="Normal" FontSize="12" Name="TextBlockC" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜