0

I have a behavior ,How can I use it in TextBlock Style so that it gets applied to all TextBlocks.

<TextBlock Grid.Row="0"  Grid.Column="1" Text="{Binding Distance}" >
        <i:Interaction.Behaviors>
            <Behaviors:EmptyToNaBehaviour/>
        </i:Interaction.Behaviors>
</TextBlock>

this is my attached behavior which is basically changing the empty value to N/A

 public class EmptyToNaBehaviour : Behavior<TextBlock>
{
    protected override void OnAttached()
    {
        var txtblock = this.AssociatedObject as TextBlock;

        if (txtblock != null && string.IsNullOrEmpty(txtblock.Text))
        {
            txtblock.Text = "N/A";
        }
    }
}
TRS
  • 1,947
  • 4
  • 26
  • 49

1 Answers1

4

There are basically two different ways of implementing behaviours in WPF, commonly referred to as attached behaviours and Blend behaviours.

An attached behaviour is simply an attached property with a PropertyChangedCallback change handler attached to it that performs some action on or extends the DependencyObject to which it is attached when the value of the dependency property changes.

You typically define an attached behaviour as a static class with a set of static methods that get and set the value of the dependency property and perform the desired logic as a result of the callback being invoked. You could easily set the value of any such properties in a Style:

<Setter Property="local:EmptyToNaBehaviour.SomeProperty" Value="x"/>

A Blend behaviour provides a better way of encapsulating the functionality of a behaviour compared to an ordinary attached behaviour. They are also more design friendly easily as they can be easily attached to visual elements in the UI via drag-drop functionality in Blend. But I am afraid you cannot really add a Blend behaviour to a Style setter:

How to add a Blend Behavior in a Style Setter

So if you want to be able to apply your behaviour in the context of a Style, you should write an attached behaviour instead of a Blend behaviour.

Edit: Your behaviour is a Blend behaviour. Replace it by an attached behaviour:

public class EmptyToNaBehaviour
{
    public static string GetText(TextBlock textBlock)
    {
        return (string)textBlock.GetValue(IsBroughtIntoViewWhenSelectedProperty);
    }

    public static void SetText(TextBlock textBlock, string value)
    {
        textBlock.SetValue(IsBroughtIntoViewWhenSelectedProperty, value);
    }

    public static readonly DependencyProperty IsBroughtIntoViewWhenSelectedProperty =
        DependencyProperty.RegisterAttached(
        "Text",
        typeof(string),
        typeof(EmptyToNaBehaviour),
        new UIPropertyMetadata(null, OnTextChanged));

    private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TextBlock txtblock = d as TextBlock;
        if(txtblock != null)
            txtblock.Loaded += Txtblock_Loaded;
    }

    private static void Txtblock_Loaded(object sender, RoutedEventArgs e)
    {
        TextBlock txtblock = sender as TextBlock;
        string text = GetText(txtblock);
        if (txtblock != null && !string.IsNullOrEmpty(text) && string.IsNullOrEmpty(txtblock.Text))
        {
            txtblock.Text = text;
        }
    }
}

Usage:

<TextBlock Grid.Row="0"  Grid.Column="1" Text="text"
           Behaviors:EmptyToNaBehaviour.Text="N/A">
</TextBlock>

Or in a Style:

<Style x:Key="style" TargetType="TextBlock">
    <Setter Property="Behaviors:EmptyToNaBehaviour.Text" Value="N/A" />
</Style>
mm8
  • 163,881
  • 10
  • 57
  • 88
  • I have added my attached behavior to question which doesn't have a dependency property,could you suggest solution to set in style in this case. – TRS Jun 08 '17 at 10:46
  • That's a Blend behaviour and as mentioned you cannot apply such a behaviour to a Style. – mm8 Jun 08 '17 at 10:47
  • But why are you even using a behaviour here? You could easily accomplish the same functionality in the Style itself. – mm8 Jun 08 '17 at 10:48
  • yeah I tried that,but I am not able to use trigger and setter on same property "Text". – TRS Jun 08 '17 at 10:53