private TextTestVM textVM = new TextTestVM();
…
txtTest.DataContext = textVM;
…
private class TextTestVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public TextTestVM()
{
}
private string text;
public string Text
{
get { return text; }
set
{
Console.WriteLine($"Text : {value}");
text = value;
OnPropertyChanged("Text");
}
}
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
위 소스와 같이 txtTest 텍스트 박스를 TextTestVM 에 양방향(TwoWay) 실시간(PropertyChanged) 바인딩을 하고
txtTest 에 사용자가 텍스트 입력을 해도 TextTestVM의 Text 프로퍼티에 실시간 입력이 되지않더군요.
실시간 입력이 된다면 Console.WriteLine($“Text : {value}”) 에서 실시간으로 입력값을 찍어야하는데
포커스를 잃을때만 호출이 됩니다.
이게 원래 TextBox 입력이 실시간(PropertyChanged)을 지원하지않아서 인가요 아니면 제가 뭔가 잘못한게 있는건가요?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace DataContextSample
{
///
/// MainWindow.xaml에 대한 상호 작용 논리
///
public partial class MainWindow : Window
{
private TextTestVM textVM = new TextTestVM();
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
txtTest.DataContext = textVM;
}
private void btnUpdateSource_Click(object sender, RoutedEventArgs e)
{
BindingExpression binding = txtWindowTitle.GetBindingExpression(TextBox.TextProperty);
binding.UpdateSource();
}
private void btnTest_Click(object sender, RoutedEventArgs e)
{
textVM.Text = "this is test";
}
private class TextTestVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public TextTestVM()
{
}
private string text;
public string Text
{
get { return text; }
set
{
Console.WriteLine($"Text : {value}");
text = value;
OnPropertyChanged("Text");
}
}
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
소스코드 올리는 법을 몰랐었어 다시 올립니다.
위에 첫번째 소스는 MainWindow.xaml 이고 그 다음이 MainWindow.xaml.cs 입니다
txtTest 부분만 보시면 됩니다.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TextBoxBind
{
/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
private string inputText;
public string InputText
{
get { return inputText; }
set
{
if (inputText != value)
{
Console.WriteLine($"InputText : {value}");
inputText = value;
OnPropertyChanged("InputText");
}
}
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
예제를 단순화해서 MainWindow에 TextBox 한개 붙이고 바인딩을 시켜도 실시간 호출이 안되네요.
그런데 신기한게 문자 입력후에 delete 키로 삭제하는 동안에는 실시간 호출이 됩니다