1
遵循MVVM模式。每當TextBox中的文本更改時,我都需要TextBox在viewModel上觸發屬性設置器。問題是ViewModel上的setter永遠不會被調用。這是我有:綁定源不更新
視圖(的.cs)
public partial class AddShowView : PhoneApplicationPage
{
public AddShowView()
{
InitializeComponent();
}
private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
{
DataContext = new AddShowViewModel(this.NavigationService);
}
private void SearchTextBox_TextChanged_1(object sender, TextChangedEventArgs e)
{
var textBox = (TextBox)sender;
var binding = textBox.GetBindingExpression(TextBox.TextProperty);
binding.UpdateSource();
}
}
視圖(的.xaml),只有相關的部分
<TextBox Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Center" Text="{Binding SearchText, UpdateSourceTrigger=Explicit}" TextChanged="SearchTextBox_TextChanged_1" />
視圖模型
public class AddShowViewModel : PageViewModel
{
#region Commands
public RelayCommand SearchCommand { get; private set; }
#endregion
#region Public Properties
private string searchText = string.Empty;
public string SearchText
{
get { return searchText; }
set
{
searchText = value;
SearchCommand.RaiseCanExecuteChanged();
}
}
#endregion
public AddShowViewModel(NavigationService navigation) : base(navigation)
{
SearchCommand = new RelayCommand(() => MessageBox.Show("Clicked!"),() => !string.IsNullOrEmpty(SearchText));
}
}
從源到目標的綁定工作,我已經雙重檢查,所以DataContext設置正確。我不知道我錯在哪裏。 感謝您的幫助。
謝謝,我認爲這是默認設置。 –
這是WPF的默認設置,不適用於Silverlight和Windows Phone。 –
您還應該在SearchText屬性 –