XAML 代码中的函数调用? [英] Function call within XAML code?
本文介绍了XAML 代码中的函数调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的所有 TextBox 控件上设置一个样式,当它接收到键盘焦点时执行以下操作:
I'd like to set a style on all my TextBox controls that does the following when it receives keyboard focus:
1) 改变背景颜色
2) 调用 .SelectAll() 来突出显示所有文本
到目前为止我有这个:
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="#FFFFD1D9"/>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
有没有办法也调用 .SelectAll()
?谢谢.
Is there a way to also call .SelectAll()
? Thanks.
推荐答案
您可以使用附加行为来做到这一点.
You can do this using attached behaviours.
示例
public static class TextBoxBehaviour
{
public static bool GetSelectAll(TextBoxBase target)
{
return (bool)target.GetValue(SelectAllAttachedProperty);
}
public static void SetSelectAll(TextBoxBase target, bool value)
{
target.SetValue(SelectAllAttachedProperty, value);
}
public static readonly DependencyProperty SelectAllAttachedProperty = DependencyProperty.RegisterAttached("SelectAll", typeof(bool), typeof(TextBoxBehaviour), new UIPropertyMetadata(false, OnSelectAllAttachedPropertyChanged));
static void OnSelectAllAttachedPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
((TextBoxBase)o).SelectAll();
}
}
使用
<Style TargetType="{x:Type TextBox}" xmlns:behaviours="clr-namespace:Controls.Behaviours">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="Background" Value="#FFFFD1D9"/>
<Setter Property="behaviours:TextBoxBehaviour.SelectAll" Value="True"/>
</Trigger>
</Style.Triggers>
</Style>
参考资料
注意:无法测试上述实现,尽管理论上它应该可以正常工作™.
NB: Wasn't able to test the above implementation, in theory though it should just work™.
HTH,
这篇关于XAML 代码中的函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文