【问题标题】:How to put behavior in style for TextBox in Silverlight?如何在 Silverlight 中为 TextBox 设置样式?
【发布时间】:2012-06-18 20:46:24
【问题描述】:

在 Xaml 中,我可以为文本框设置自定义行为,例如:

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

我希望所有的TextBox都有这种行为,那么如何把这种行为放在隐式风格之类的呢?

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

更新: 感谢您的信息。尝试以下建议的方式,应用程序崩溃:

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

我的行为是这样的:

 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 看起来不像是智能出来的。

【问题讨论】:

  • 你说“app is crashed”什么是异常信息,stacktrace?
  • 它说Can not cast the type MyNameSpace:TextBoxNewBehavior to type ...。但是如果我直接把它放在xaml中,没问题。困惑。
  • 似乎没那么容易......但我在这里发现了同样的问题,所以我投票结束这个问题。
  • 它不是重复的 - 这是用于 WPF

标签: silverlight silverlight-4.0 silverlight-5.0


【解决方案1】:

运行时错误说明

<Setter Property="i:Interaction.Behaviors">
    <Setter.Value>
        <My:TextBoxNewBehavior/>
    </Setter.Value>
</Setter>
  1. 您不能同时将行为附加到不同的对象。
  2. Interaction.Behaviors 是只读集合,您无法设置。

写作

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

表示使用 XAML 中的隐式集合语法,即在 Behaviors 集合上调用 Add()。

解决方案

使用样式设置器编写您自己的附加属性,如下所示:

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

然后你可以在附加的属性代码中创建和设置行为:

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*/ }
}

【讨论】:

  • 如果你这样做,还不如在新 TBlock 的构造函数中完成整个事情......在尝试设置风格行为一段时间后,似乎是最不麻烦的方式
【解决方案2】:

我已经在 Windows 10 项目中解决了它,但它应该与 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>

如果我以任何其他方式编写它不起作用,但作为资源,集合可以工作!

【讨论】:

    猜你喜欢
    • 2014-01-10
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    相关资源
    最近更新 更多