<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似乎没有出现在情报中.
解决方法 运行时错误的说明<Setter Property="i:Interaction.Behaviors"> <Setter.Value> <My:TextBoxNewBehavior/> </Setter.Value></Setter>
>您无法同时将行为附加到不同的对象.
> 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*/ }}总结
以上是内存溢出为你收集整理的如何在Silverlight中为TextBox设置样式?全部内容,希望文章能够帮你解决如何在Silverlight中为TextBox设置样式?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)