开发者

Can MVVM Usercontrols have property defined in codebehind?

I have a WPF user control ...which is in MVVM. The user control(which contains a listview)开发者_Go百科 need data from the page (where it is included). I have to set a property to get this data input. Will this comply with MVVM...if not, what is the way for the same?


I'm afraid this won't be correct in MVVM design pattern. try to stick to your view model to define properties. Why don't you consider moving that property to control's vm?


Use an ObservableCollection rather.

ObservableCollection<myModel> myOC = new ObservableCollection<myModel>();

where myModel is a class that has to be constructed transforming your columns in the DataTable to Properties.

In your MainViewModel, loop through the DataReader and create myOC out of it.

Now bind myOC to a ListView in your page.

The DataTemplate of ListView should be a view(UserControl) drawing data from a ViewModel constructed out of myModel

But your UserControl has the entire ListView inside. If that is on purpose, then let me know the entire design to give a better idea.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜