如何自动更改silverlight文本框中的文本?

时间:2012-10-23 20:41:22

标签: c# silverlight xaml mvvm caliburn.micro

我在Silverlight 5项目中使用MVVM / Caliburn.Micro,我需要自动将用户在silverlight文本框中输入的文本更改为大写。

首先,我认为我可以将ViewModel上的支持变量设置为大写,双向绑定将更改文本。这不起作用(虽然我相信如果我使用丢失的焦点事件,但我不能这样做,因为我还有其他事情我必须为KeyUp做,并附加两个事件导致xaml错误)

由于这不起作用,我尝试在KeyUp事件上调用一个方法。这个技术上有效,但由于它正在替换文本,它会将光标放回到开头,因此用户最终会向后键入。

这似乎是相当简单的功能 - 如何将用户键入的文本转换为大写?我错过了一些简单的东西吗?

这是我现有的代码。 XAML:

<TextBox x:Name="SomeName" cal:Message.Attach="[Event KeyUp] = [Action ConvertToUppercase($eventArgs)]" />

查看型号:

public void ConvertToUppercase(System.Windows.Input.KeyEventArgs e)
{
     SomeName = _someName.ToUpper();
     //Other code that doesn't concern uppercase
}



编辑替代解决方案: McAden提出了一个很好的通用解决方案。我几乎同时意识到有一个替代解决方案(只需将文本框作为参数传递给大写方法并移动光标),所以这里也是代码:

XAML:

<TextBox x:Name="SomeName" cal:Message.Attach="[Event KeyUp] = [Action ConvertToUppercase($source, $eventArgs)]; [Event KeyDown] = [Action DoOtherStuffThatIsntQuestionSpecific($eventArgs)]" />

cs方法:

public void ConvertToUppercase(TextBox textBox, System.Windows.Input.KeyEventArgs e)
{
    //set our public property here again so the textbox sees the Caliburn.Micro INPC notification fired in the public setter
    SomeName = _someName.ToUpper();

    //move the cursor to the last so the user can keep typing
    textBox.Select(SomeName.Length, 0);
}

当然还有cs标准Caliburn.Micro属性:

private String _someName = "";
public String SomeName
{
    get
    {
        return _someName;
    }
    set
    {
        _someName = value;
        NotifyOfPropertyChange(() => SomeName);
    }
}

2 个答案:

答案 0 :(得分:4)

按照提到的here创建一个ToUpper EventTrigger。同时为你想要完成的任何其他功能创建另一个。在xaml中添加它们:

<TextBox Text="{Binding SomeName, Mode=TwoWay}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="TextChanged">
            <myBehaviors:UpperCaseAction/>
        </i:EventTrigger>
        <i:EventTrigger EventName="TextChanged">
            <myBehaviors:MyOtherAction/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>

编辑:我已使用以下内容对此解决方案进行了全面测试(没有代码隐藏

UpperCase动作:

public class UpperCaseAction : TriggerAction<TextBox>
{
    protected override void Invoke(object parameter)
    {
        var selectionStart = AssociatedObject.SelectionStart;
        var selectionLength = AssociatedObject.SelectionLength;
        AssociatedObject.Text = AssociatedObject.Text.ToUpper();
        AssociatedObject.SelectionStart = selectionStart;
        AssociatedObject.SelectionLength = selectionLength;
    }
}

其他行动:

public class OtherAction : TriggerAction<TextBox>
{
    Random test = new Random();

    protected override void Invoke(object parameter)
    {
        AssociatedObject.FontSize = test.Next(9, 13);
    }
}

XAML命名空间(在这种情况下, TestSL是我的测试项目的命名空间 - 适当地使用您的命名空间):

xmlns:local="clr-namespace:TestSL"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

XAML TextBox

<Grid x:Name="LayoutRoot" Background="LightGray" Width="300" Height="200">
    <TextBox TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10" Width="100">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="TextChanged">
                <local:UpperCaseAction />
            </i:EventTrigger>
            <i:EventTrigger EventName="TextChanged">
                <local:OtherAction />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </TextBox>
</Grid>

答案 1 :(得分:1)

UpperCaseConverter.cs:

namespace MyProject.Converters
    {
        /// <summary>
        /// A upper case converter for string values.
        /// </summary>
        public class UpperCaseConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return ConvertToUpper(value);
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return ConvertToUpper(value);
            }

            private string ConvertToUpper(object value)
            {
                if (value != null)
                {
                    return value.ToString().ToUpper();
                }
                return null;
            }
        }
    }

AppResources.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:conv="clr-namespace:MyProject.Converters;assembly=MyProject"
    mc:Ignorable="d"
    >

    <!-- Converters -->
    <conv:UpperCaseConverter x:Key="UpperCaseConverter"/>

</ResourceDictionary>

MyFormView.xaml:

<UserControl>
    <TextBox Text="{Binding myText, Mode=TwoWay, Converter={StaticResource UpperCaseConverter}}" />
</UserControl>