有了文本框,我想将其文本值发送到视图模型:
<TextBox Name="txSourceText" Text="{Binding UploadFilePath,Mode=TwoWay}" />
同时我想从代码隐藏的方法中设置文本框的值:
private void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofp = new OpenFileDialog();
ofp.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
ofp.FilterIndex = 1;
bool? userClickedOK = ofp.ShowDialog();
if (userClickedOK == true)
{
//PathToFile = ofp.FileName;
***txSourceText.Text = ofp.FileName;***
}
}
我该怎么做?
答案 0 :(得分:0)
查看ICommand
界面以及如何在MVVM中使用Command
。
http://www.codeproject.com/Tips/813345/Basic-MVVM-and-ICommand-Usage-Example
ViewModel
属性Command
对象,然后将ViewModel
添加为属性Command
属性答案 1 :(得分:0)
您可以使用TextBox_TextChanged事件将文本发送到ViewModel中的属性。在那种情况下,您可以访问窗口的datacontext,从中可以设置viewmodel的属性
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
var viewModel = this.DataContext;
(this.DataContext as ViewModel).MyProperty = text1.Text;
}