2013-04-24 103 views
2

我有一个DataTemplate一个TextBox声明如下:如何清除MVVM中的文本框?

<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> 

什么这个文本框通常做的就是按下Enter键或失去焦点时,当执行MVVM光强RelayCommand。

我的问题是,我无法弄清MVVM在上述两种情况下通过XAML清除文本框的文本值的方法。在代码隐藏方面非常简单,但我无法在MVVM中找到它。

任何想法?

+0

为“TextBox”的Text属性创建一个绑定到ViewModel中的属性,将属性设置为“”,并且你的“TextBox”文本将被清除? – Viv 2013-04-24 15:53:13

+0

我无法在ViewModel中使用它,我的行被绑定到一个DataView,这会要求我为此专门设置一列。但是,谢谢你的回复:) – user2170838 2013-04-24 16:40:28

+0

@ user2170838不要忘记你可以使用类似于'RelativeSource'绑定的东西来绑定到当前DataContext以外的东西,比如'Text =“{Binding RelativeSource = {RelativeSource AncestorType = {x :Type DataGrid}},Path = DataContext.NewNoteText}“',DataGrid的'DataContext'同时包含DataView和新的注释文本的字符串属性。当然,这只适用于文本是静态的,无论点击哪一行:) – Rachel 2013-04-24 16:44:00

回答

2

如果文本数据层和应用逻辑的一部分,一个字符串应该存在于您的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; 
} 
+0

我担心使用代码隐藏会击败MVVM的目的,但你是对的它不会与ViewModel交互,所以它应该是完全正确的:) – user2170838 2013-04-24 16:41:22

0

您可以使用UpdateSourceHelper。这真的帮我调出了一个没有代码隐藏的事件。 看到这里http://www.wiredprairie.us/blog/index.php/archives/1701

所有你需要做的就是创建一个类“UpdateSourceHelper”,用你的XAML这样

xmlns:local="using:WiredPrairie.Converter 

连接,并将其绑定到文本框(或任何你要绑定) ...

<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}"/> 

如果你想打电话LostFocus事件的助手,你只需要在你的助手添加这两条线:

tb.LostFocus += AttachedTextBoxLostFocus; 
tb.LostFocus -= AttachedTextBoxLostFocus; 

因此,这将是这样的:

TextBox tb = (TextBox)obj; 
    if ((bool)args.NewValue) 
     {       
      tb.LostFocus += AttachedTextBoxLostFocus; 
     } 
    else 
     { 
      tb.LostFocus -= AttachedTextBoxLostFocus; 
     } 

右键单击AttachedTextBoxLostFocus和产生方法。现在,您可以像处理代码事件一样处理事件。