다음 샘플에서는 TextBox에 새 문자열을 입력하고 TabBlock을 눌렀을 때 TextBlock이 업데이트되지만 TextBox는 입력 한 값을 유지하면서 대신 수정 된 문자열로 업데이트됩니다 . 어떤 생각이 행동을 변경하는 방법?포커스 손실 후 TextBox의 TwoWay 바인딩이 업데이트되지 않습니다.
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}" Margin="106,240,261,187">
<StackPanel>
<TextBox Text="{Binding MyProp, Mode=TwoWay}"/>
<TextBlock Text="{Binding MyProp}"/>
</StackPanel>
</Grid>
</Page>
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public ViewModel()
{
MyProp = "asdf";
}
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
protected bool SetField<T>(ref T field, T value, string propertyName)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
private string m_myProp;
public string MyProp
{
get { return m_myProp; }
set
{
m_myProp = value + "1";
OnPropertyChanged("MyProp");
}
}
}
덕분에 윈도우 폰 7이 "문제"가없는 나는 결국 이 코드를 사용하여 : CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync ( Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler (>) { MyProp = newValue; }})); – eih
위대한 설명! 내 뷰 모델에는 Dispatcher가 없으므로 SynchronizationContext.Current.Post 메서드를 사용하여 바인딩 컨텍스트 외부에서 작업을 "대기열에 추가"했습니다. – Samuel