This might be trivial question, but just can't find good solution to solve this. I do have a usercontrol(MyUserControl) that is duplicated in the mainview. One instance of usercontrol presents source object and one presents target object. Views are similar, but I need to know which is source and which is target in the usercontrol's viewmodel. MainView has SourcenContent and TargetContent.So question is that how I can separate SourcenContent and TargetContent in the usercontrol's viewmodel?
MainWindow `
<DockPanel LastChildFill="True">
<Border Height="400" DockPanel.Dock="Top">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="400"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
</Grid.RowDefinitions>
<GroupBox Header="Source Database" Grid.Row="0" Grid.Column="0">
<StackPanel Orientation="Vertical" >
<ContentControl Content="{Binding SourcenContent}" Name="source" VerticalAlignment="Stretch"/>
</StackPanel>
</GroupBox>
<GroupBox Header="Target Database" Grid.Row="0" Grid.Column="1">
<StackPanel Orientation="Vertical">
<ContentControl Content="{Binding TargetContent}" Name="Target" VerticalAlignment="Stretch"/>
</StackPanel>
</GroupBox>
</Grid>
</Border>
...
</DockPanel>
`
MainViewModel
public class MainViewModel : ViewModelBase
{
private UserControl _sourcenContent;
private UserControl _targetContent;
private MyUserControl _sourcenContentUserControl;
private MyUserControl _targetContentUserControl;
public MainViewModel()
{
_sourcenContentUserControl = new MyUserControl();
_sourcenContent = _sourcenContentUserControl;
_targetContentUserControl = new MyUserControl();
_targetContent = _targetContentUserControl;
}
...
public UserControl SourcenContent
{
get { return _sourcenContent; }
set
{
if (_sourcenContent != value)
{
_sourcenContent = value;
RaisePropertyChanged("SourcenContent");
}
}
}
public UserControl TargetContent
{
get { return _targetContent; }
set
{
if (_targetContent != value)
{
_targetContent = value;
RaisePropertyChanged("TargetDatabaseConnectionContent");
}
ViewModelLocator
public class ViewModelLocator
{
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
SimpleIoc.Default.Register<MainViewModel>();
SimpleIoc.Default.Register<MyUserControl>();
}
public MainViewModel MainViewModel
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
public MyUserControlViewModel MyUserControlVM
{
get
{
return ServiceLocator.Current.GetInstance<MyUserControlViewModel>(Guid.NewGuid().ToString());
}
}
}
Usercontrol
<Grid Margin="8">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="75"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" >
... Textboxes and other input controls
enter code here
MyUserControlViewModel
public class MyUserControlViewModel : ViewModelBase
{
public MyUserControlViewModel(IService service, ...)
{
**/* How do I know which user control created this SourcenContent or TargetContent*/**
}
Yes xaml binding is/was confusing me and the answer to my question was very trivial. If I access viewModelLocator from code side and not try to do binding in XAML MainViewModel:
{ //Get access to the ViewModelLocator ViewModelLocator viewModelLocator = System.Windows.Application.Current.Resources["Locator"] as ViewModelLocator;
}
ViewModelLocator