更新时间:2023-10-23 12:53:28
您可以通过创建行为来实现这一点,如下所示:
You can achieve this by creating a behavior, like this:
public class UpperCaseAction : TriggerAction<TextBox>
{
protected override void Invoke(object parameter)
{
var selectionStart = AssociatedObject.SelectionStart;
var selectionLenght = AssociatedObject.SelectionLength;
AssociatedObject.Text = AssociatedObject.Text.ToUpper();
AssociatedObject.SelectionStart = selectionStart;
AssociatedObject.SelectionLength = selectionLenght;
}
}
然后,在您的 TextBox 中使用它,如下所示:
Then, use it in your TextBox, like this:
<Grid x:Name="LayoutRoot" Background="White">
<TextBox TextWrapping="Wrap" VerticalAlignment="Top" Margin="10">
<i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged">
<ASD_Answer009_Behaviors:UpperCaseAction/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
</Grid>
其中 i:
是
clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity
背后的代码:
System.Windows.Interactivity.EventTrigger eventTrigger = new System.Windows.Interactivity.EventTrigger("TextChanged");
eventTrigger.Actions.Add(new UpperCaseAction());
System.Windows.Interactivity.Interaction.GetTriggers(myTextBox).Add(eventTrigger);
为了创建和使用行为,您需要下载并安装Expression Blend SDK for Silverlight 4 并添加对 System.Windows.Interactivity.dll.
In order to create and use behaviors, you need to download and install the Expression Blend SDK for Silverlight 4 and add a reference to System.Windows.Interactivity.dll.