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()以突出显示所有文本
到目前为止,我已经知道了:
I have this so far:
<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>
参考文献
- Josh Smith on Attached Behaviours
NB:理论上虽然可以正常工作,但它无法测试上述实现。
NB: Wasn't able to test the above implementation, in theory though it should just work™.
HTH,
这篇关于XAML代码中的函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文