我有一個WPF視圖模型WPF文本框不會在視圖模型
class MainWindowViewModel : INotifyPropertyChanged
{
private string _sql;
public string Sql
{
get { return _sql; }
set
{
if (value == _sql) return;
OnPropertyChanged("Sql");
_sql = value;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
我也有一個XAML視圖中,帶有文本框
<Window.Resources>
<HbmSchemaExporter:MainWindowViewModel x:Key="viewModel"/>
</Window.Resources>
....
<TextBox Grid.Row="6" Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding Source={StaticResource ResourceKey=viewModel}, Path=Sql,Mode=OneWay}"/>
代碼背後
private MainWindowViewModel ViewModel
{
get { return Resources["viewModel"] as MainWindowViewModel; }
}
問題是,當我在代碼viewModel.Sql = SOMETHING
的文本框沒有得到更新。調試器在屬性中顯示正確的值,但文本框保持空白。
我也嘗試將綁定更改爲TwoWay
,但只允許我用我在文本框中輸入的值覆蓋該屬性,這是我真的不想要的(實際上我仍然需要使其只讀,但它目前超出了範圍)。
如何在編程更新屬性後更新文本框?
該應用程序基本上是一個NHibernate DDL生成器,我在閱讀this後寫入。我需要按「生成SQL」按鈕,它顯示要運行到數據庫的代碼。
OP評論:一旦我將使應用程序正常工作,而不僅僅是一個程序集,我可能會開源它 – 2013-03-16 21:36:11