Hi I'm new to silverlight and MVVM logic, I've readed many articles, blogs and etc ..., but many things they've explained are about how to dealing with database operation. Let's say I have a image control and button which should upload a file and also shows selected picture in appropriate control. I don't know how to do this with MVVM pattern. I don't want you to describe how to upload file with silverligh开发者_高级运维t, actually the problem is I don't know how should I access to image control in ViewModel class to set its source property.
Any advice will be grateful Best Regards.
You don't access controls in the view-model, you expose properties.
The view, in turn, binds to the properties exposed by the view-model. In MVVM, the view's DataContext
is set to a view-model.
View:
<Window … namespaces, etc. />
<Grid>
<TextBox Text={Binding InputText, Mode=TwoWay}
</Grid>
</Window>
ViewModel:
public class MyViewModel : INotifyPropertyChanged
{
string _text = "Enter text here";
public string Text
{
get { return _text; }
set
{
_text = value;
// raise property change notification
}
}
// implement INPC so the view will know when the view-model has changed
}
Now if you set the view window's DataContext
property to an instance of MyViewModel
, the textbox will contain the text "Enter text here," because its Text
property is bound to the InputText
property. If you type something else in the textbox, the view-model's InputText
property will be updated to that value.
精彩评论