wpf 如何清除 MVVM 中的文本框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16196466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to clear a TextBox in MVVM?
提问by user2170838
I have a TextBox in a DataTemplate declared as follows:
我在 DataTemplate 中有一个 TextBox,声明如下:
<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.
这个 TextBox 的主要作用是在按下 Enter 键或失去焦点时执行 MVVM-Light RelayCommand。
My problem is that I cannot figure out a way in MVVM to clear the TextBox's Textvalue through XAML in the above two scenarios. It's very easy with in code-behind, but I can't figure it out in MVVM.
我的问题是,在上述两种情况下,我无法在 MVVM 中找到通过 XAML清除 TextBox 的Text值的方法。在代码隐藏中很容易,但我无法在 MVVM 中弄清楚。
Any ideas?
有任何想法吗?
回答by Rachel
If the text is part of your data layer and application logic, a string should exist in your Modelor ViewModeland be cleared from there
如果文本是您的数据层和应用程序逻辑的一部分,则您的Model或中应该存在一个字符串ViewModel并从那里清除
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.
如果它只是 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;
}
回答by Rudi
You can use a UpdateSourceHelper. This really helped me out calling an event with no code-behind. See here http://www.wiredprairie.us/blog/index.php/archives/1701
您可以使用 UpdateSourceHelper。这真的帮助我调用了一个没有代码隐藏的事件。见这里http://www.wiredprairie.us/blog/index.php/archives/1701
All you have to do is create a class "UpdateSourceHelper", connect it with your xaml like this
你所要做的就是创建一个类“UpdateSourceHelper”,像这样将它与你的xaml连接起来
xmlns:local="using:WiredPrairie.Converter
and bind it to your TextBox (or whatever you want to bind to)...
并将其绑定到您的 TextBox(或您想要绑定到的任何内容)...
<TextBox Height="Auto" Margin="0,6" Grid.Row="1" TextWrapping="Wrap" TabIndex="0"
Text="{Binding Value}"
local:UpdateSourceHelper.IsEnabled="True"
local:UpdateSourceHelper.UpdateSourceText="{Binding Value, Mode=TwoWay}"/>
If you want call LostFocus Event in the Helper, you simply have to add these 2 lines in your Helper:
如果您想在 Helper 中调用 LostFocus 事件,您只需在 Helper 中添加以下两行:
tb.LostFocus += AttachedTextBoxLostFocus;
tb.LostFocus -= AttachedTextBoxLostFocus;
So it would look like this:
所以它看起来像这样:
TextBox tb = (TextBox)obj;
if ((bool)args.NewValue)
{
tb.LostFocus += AttachedTextBoxLostFocus;
}
else
{
tb.LostFocus -= AttachedTextBoxLostFocus;
}
Right click on AttachedTextBoxLostFocus and generate the method. Now you can handle the Event like a code-behind event.
右键单击 AttachedTextBoxLostFocus 并生成方法。现在您可以像处理代码隐藏事件一样处理事件。

