I need create a Property in one of my user controls of a Model type but i think i must prevent direct access to Model Layer from View Layer.
I have a View Model of the Model that provide set of my model objects...
- SetOfA_UserControl
- SetOfA_ViewModel
- A_Model
I need a property Like this in my user control:
public A_Model SelectedA { get; set; }
One way is create a new View Model like following codes and use it in my User Control:
// ------------ View Model Layer ------------
public class SingleA_ViewModel: ModelA
{
}
// --------------- View Layer ---------------
public SingleA_ViewModel SelectedA { get; set; }
But I'm trying to prevent a new empty view model class that inherit the model like above. is it correct?
What is your suggestions to prevent direct access to Model layer and create the Property in my User Control???
EDIT 1:
I have 3 project:
- View Project
- View Model Project
- Model Project
I want know can i prevent reference to Model project in View project or not....
I have a SelectedA property in my View Model too and i put my logic in View model class and it work well in my view but also i have a SelectedA Property in my UserControl that i bind it to SelectedA property in my ViewModel class... But i need direct access to Model in UserControl to define this property!
When i have a direct access to Model from View my codes is like this:
// ------------ Model Layer ------------
public class AModel
{
}
// ------------ View Model Layer ------------
public class SetOfA_ViewModel: INotifyPropertyChanged
{
public AModel SelectedA { get; set; }
public ObservableCollection<AModel> Items
{
get { return _items; }
set
{
_items = value;
OnPropertyChanged("Items");
}
}
// Other Logic codes to fill and keep SelectedA value and....
}
// --------------- View Layer ---------------
public partial class MyUserControl : UserControl
{
public AModel SelectedA {
get { return (AModel)GetValue(SelectedAProperty); }
set
{
var oldValue = (AModel)GetValue(SelectedAProperty);
if (oldValue != value) SetValue(SelectedAProperty, value);
}
}
public static readonly DependencyProperty SelectedAProperty =
DependencyProperty.Register(
"SelectedA",
typeof(AModel),
typeof(MyUserControl),
new PropertyMetadata(OnSelectedAValueChanged));
public MyUserControl ()
{
InitializeComponent();
const string NAME_OF_PROPERTY_IN_VM = "SelectedA";
var binding = new Binding(NAME_OF_PROPERTY_IN_VM) { Mode = BindingMode.TwoWay };
this.SetBinding(SelectedAProperty, binding);
}
private static void OnSelectedAValueChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
//------
}
}
The above method work well for me and i use this now BUT I'm trying to Delete any direct access and reference to Model Project in my View Project then how can i implement the SelectedA dependency property of AModel in my user control?
Some friends say you can access model project directly from view project. i want know Correct ways, NOT possible ways...!
Edit 2
When i keep SelectedA item in my user control then use it in my window like this:
<userControls:MyUserControl x:Name="MyUserControl1"/>
<Label Content="{Binding ElementName=MyUserControl1, Path=SelectedA.Title}" />
Edit 3
Why i want prevent direct access to Model from ViewModel?
I searched about MVVM diagrams and did not find and direct access from View to Model. all the diagrams says:

..........
Now can we direct access to model from view yet?
Why there are many samples that have direct access to model in View on the web?
And why some people say you can do this?
If we can do this and direct access to model is a correct implement why there is not any relation between View and Model in the above diagrams???