如何清除MVVM中的文本框? [英] How to clear a TextBox in MVVM?

查看:85
本文介绍了如何清除MVVM中的文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在DataTemplate中有一个文本框,声明如下:

<TextBox Grid.Row="1" Grid.Column="1" Margin="0,4,0,0">
<i:Interaction.Triggers>
    <i:EventTrigger EventName="LostFocus">
        <cmd:EventToCommand Command="{Binding DataContext.NotesEnteredCommand,
                            RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}">
            <cmd:EventToCommand.CommandParameter>
                <MultiBinding Converter="{StaticResource SimpleMultiValueConverter}">
                    <Binding Path="Row.OID" />
                    <Binding Path="Text" RelativeSource="{RelativeSource FindAncestor, AncestorType=TextBox}" />
                </MultiBinding>
            </cmd:EventToCommand.CommandParameter>
        </cmd:EventToCommand>
    </i:EventTrigger>
</i:Interaction.Triggers>

<TextBox.InputBindings>
    <KeyBinding Key="Enter" Command="{Binding DataContext.NotesEnteredCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}">
        <KeyBinding.CommandParameter>
            <MultiBinding Converter="{StaticResource SimpleMultiValueConverter}">
                <Binding Path="Row.OID" />
                <Binding Path="Text" RelativeSource="{RelativeSource FindAncestor, AncestorType=TextBox}" />
            </MultiBinding>
        </KeyBinding.CommandParameter>
    </KeyBinding>
</TextBox.InputBindings>

此TextBox的基本功能是在按下Enter键或失去焦点时执行MVVM-Light RelayCommand.

我的问题是,在上述两种情况下,我无法在MVVM中找到一种通过XAML清除TextBox的 Text 值的方法.使用代码隐藏非常容易,但是我无法在MVVM中弄清楚.

有什么想法吗?

解决方案

如果文本是数据层和应用程序逻辑的一部分,则ModelViewModel中应存在一个字符串,并从该字符串中清除 >

例如,

<TextBox Text="{Binding NewNote}" ... />

void NotesEntered(int oid)
{
    SaveNewNote(oid);

    NewNote = string.Empty;
}

如果仅是UI层的一部分,则应使用代码隐藏功能将其清除.在UI背后的代码中包含特定于UI的逻辑是完全可以接受的,因为这仍然可以保持层的分离.

NewNoteTextBox_LostFocus(object sender, EventArgs e)
{
    (sender as TextBox).Text = string.Empty;
}

NewNoteTextBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Keys.Enter)
        (sender as TextBox).Text = string.Empty;
}

I have a TextBox in a DataTemplate declared as follows:

<TextBox Grid.Row="1" Grid.Column="1" Margin="0,4,0,0">
<i:Interaction.Triggers>
    <i:EventTrigger EventName="LostFocus">
        <cmd:EventToCommand Command="{Binding DataContext.NotesEnteredCommand,
                            RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}">
            <cmd:EventToCommand.CommandParameter>
                <MultiBinding Converter="{StaticResource SimpleMultiValueConverter}">
                    <Binding Path="Row.OID" />
                    <Binding Path="Text" RelativeSource="{RelativeSource FindAncestor, AncestorType=TextBox}" />
                </MultiBinding>
            </cmd:EventToCommand.CommandParameter>
        </cmd:EventToCommand>
    </i:EventTrigger>
</i:Interaction.Triggers>

<TextBox.InputBindings>
    <KeyBinding Key="Enter" Command="{Binding DataContext.NotesEnteredCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}">
        <KeyBinding.CommandParameter>
            <MultiBinding Converter="{StaticResource SimpleMultiValueConverter}">
                <Binding Path="Row.OID" />
                <Binding Path="Text" RelativeSource="{RelativeSource FindAncestor, AncestorType=TextBox}" />
            </MultiBinding>
        </KeyBinding.CommandParameter>
    </KeyBinding>
</TextBox.InputBindings>

What this TextBox basically does is execute a MVVM-Light RelayCommand when the Enter key is pressed or when losing focus.

My problem is that I cannot figure out a way in MVVM to clear the TextBox's Text value through XAML in the above two scenarios. It's very easy with in code-behind, but I can't figure it out in MVVM.

Any ideas?

解决方案

If the text is part of your data layer and application logic, a string should exist in your Model or ViewModel and be cleared from there

For example,

<TextBox Text="{Binding NewNote}" ... />

and

void NotesEntered(int oid)
{
    SaveNewNote(oid);

    NewNote = string.Empty;
}

If it's part of the UI layer only, it should just be cleared with code-behind. It's perfectly acceptable to have UI-specific logic in the code-behind the UI, as that still maintains the separation of layers.

NewNoteTextBox_LostFocus(object sender, EventArgs e)
{
    (sender as TextBox).Text = string.Empty;
}

NewNoteTextBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Keys.Enter)
        (sender as TextBox).Text = string.Empty;
}

这篇关于如何清除MVVM中的文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆