4

In Xaml, I can put customized behavior for a textbox like:

<TextBox>
   <i:Interaction.Behaviors>
       <My:TextBoxNewBehavior/>
   </i:Interaction.Behaviors>
</TextBox>

I want to all TextBox has this behavior, so how to put this behavior in implicit style like?

<Style TargetType="TextBox">
    <Setter Property="BorderThickness" Value="1"/>
    ....
</Style> 

Update: Thanks for info. Try the way as suggested below and the app is crashed:

<Setter Property="i:Interaction.Behaviors">
    <Setter.Value>
        <My:TextBoxNewBehavior/>
    </Setter.Value>
</Setter>

My behavior is something like:

 public class TextBoxMyBehavior : Behavior<TextBox>
    {
        public TextBoxMyBehavior()
        {
        }

        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.KeyUp += new System.Windows.Input.KeyEventHandler(AssociatedObject_KeyUp);
        }

        void AssociatedObject_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                //....
            }
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.KeyUp -= new System.Windows.Input.KeyEventHandler(AssociatedObject_KeyUp);
        }
    }

TextBoxMyBehavior looks like not coming out in intelligence.

KentZhou
  • 24,805
  • 41
  • 134
  • 200
  • You said "app is crashed" what is the exception message, stacktrace? – nemesv Jun 15 '12 at 19:33
  • it said Can not cast the type MyNameSpace:TextBoxNewBehavior to type .... But if I put it in xaml directly, no problem. Confused. – KentZhou Jun 15 '12 at 19:43
  • It seems it's not that easy... but I found the same question here so I'm voting to close this one. – nemesv Jun 15 '12 at 19:48
  • possible duplicate of [How to add a Blend Behavior in a Style Setter](http://stackoverflow.com/questions/1647815/how-to-add-a-blend-behavior-in-a-style-setter) – nemesv Jun 15 '12 at 19:48
  • it's not a duplicate - that's for WPF – SturmUndDrang Sep 20 '12 at 14:45
  • Please check it http://stackoverflow.com/questions/1647815/how-to-add-a-blend-behavior-in-a-style-setter – Kreol Oct 19 '12 at 14:46

2 Answers2

4

Explanation of runtime error

<Setter Property="i:Interaction.Behaviors">
    <Setter.Value>
        <My:TextBoxNewBehavior/>
    </Setter.Value>
</Setter>
  1. You cannot attach a behavior to different objects at the same time.
  2. Interaction.Behaviors is a read-only collection that you cannot set.

Writing

<i:Interaction.Behaviors>
     <My:TextBoxNewBehavior/>
</i:Interaction.Behaviors>

means using the implicit collection syntax in XAML, which calls Add() on the Behaviors collection.

Solution

Write you own attached property that you set using the style setter like this:

<Setter Property="my:TextBoxOptions.UseMyBehavior" Value="true" />

Then you can create and set the behavior in the attached property code:

private static void OnUseMyBehaviorPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
    if (e.NewValue.Equals(true))
        Interaction.GetBehaviors(dependencyObject).Add(new TextBoxNewBehavior());
    else { /*remove from behaviors if needed*/ }
}
Benjamin
  • 1,165
  • 7
  • 19
  • If you're going this way might as well just do the whole thing in the constructor of the new TBlock... which after playing for a while attempting to set a behaviour in style, seems to be the least cumbersome way – Oyiwai Oct 01 '17 at 12:33
0

I have solved it in a Windows 10 project but and it should be compatible for SL.

<Page.Resources>
    <i:BehaviorCollection x:Key="behaviors">
        <core:EventTriggerBehavior EventName="Tapped">
            <core:InvokeCommandAction Command="{Binding SetModeToAll}" />
        </core:EventTriggerBehavior>
    </i:BehaviorCollection>

    <Style TargetType="TextBlock" x:Key="textblockstyle">
        <Setter Property="i:Interaction.Behaviors" Value="{StaticResource behaviors}">
        </Setter>
    </Style>
</Page.Resources>

<Grid x:Name="LayoutRoot" Background="Transparent">
    <TextBlock Text="Testing" Foreground="Red" FontSize="20" Style="{StaticResource textblockstyle}">
    </TextBlock >
</Grid>

If I write in any other way it does not work, but as a resource the collection works!

Juan Pablo Garcia Coello
  • 3,192
  • 1
  • 23
  • 33