У меня есть TextBox в 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>
Что это TextBox в основном делает это выполнить MVVM-Light RelayCommand когда клавиша Enter нажата или при потере фокуса.
Моя проблема заключается в том, что я не могу определить способ в MVVM, чтобы очистить значение TextBox Text через XAML в двух вышеупомянутых сценариях. Это очень легко с кодом, но я не могу понять это в MVVM.
Любые идеи?
Создайте привязку для свойства 'Text'' TextBox' к свойству в ViewModel, установите свойство "" и текст 'TextBox' будет очищен? – Viv
Я не могу иметь его в ViewModel, мои строки привязаны к DataView, для чего мне потребуется столбец только для этой цели. Но спасибо за ответ :) – user2170838
@ user2170838 Не забывайте, что вы можете использовать что-то вроде привязки RelativeSource для привязки к чему-то другому, кроме текущего DataContext, например, «Text =» {Binding RelativeSource = {RelativeSource AncestorType = {x : Введите DataGrid}}, Path = DataContext.NewNoteText} ", где' DataContext' вашего 'DataGrid' содержит как' DataView', так и свойство string для нового текста заметки.Конечно, это работает только в том случае, если текст статичен независимо от того, на какую строку нажата :) – Rachel