Hi Abel,
Since this seems to be a common question, let me elaborate a bit. Maybe this will also help others looking at this post. MVVM is an architectural programming pattern that is used in most WPF based large scale or enterprise systems. I think in the long run a good understanding on what it does will make programming using MVVM a lot easier. I used WinForms for many years (if not decades) and it took me a while to get used to MVVM but after I got my head around it, I actually prefer it now. If you have some time maybe you can look at this YouTube: WPF: why MVVM? The video explains the "why?", but also looks a bit behind the curtain of MVVM.
Specifically to answer your question: controlling the visibility of controls on your dockpane:
Since you already know that you need to bind the Visibility attribute of your UI control, I want to mention the approach that I often use when looking for a reference sample implementation:
On the top left of the community samples github repo page I enter the text string for which I need a code snippet for (as specific as possible to reduce the number of candidates). So for your requirements I searched for 'Visibility="{Binding' (note: only search for this repo not all of Github):
In similar fashion you can also search https://github.com/Esri/arcgis-pro-sdk because it contains the source for all ProSnippets. The search above yields all controls that are bound to some kind of 'visibility' property in my viewmodel. I open the "Samples" solution (from community samples) in Visual Studio and look at the located solutions there. In this case I search in Visual Studio for '<Grid Visibility="{Binding DockpaneVisibility}">' which yields the 'SymbolLookup' project. I open the ViewModel file: SymbolLookupDockpaneViewModel.cs and the View file: SymbolLookupDockpane.xaml.
Databinding links the 'DockpaneVisibility' property:
private Visibility _dockpaneVisibility;
public Visibility DockpaneVisibility
{
get { return _dockpaneVisibility; }
set { SetProperty(ref _dockpaneVisibility, value, () => DockpaneVisibility); }
}
To this UI element:
<Grid Visibility="{Binding DockpaneVisibility}">
Note that 'SetProperty(ref...' the DockpaneVisibility setter is required because this method will 'notify' the UI (via data binding) when the code changes the DockpaneVisibility property.
To change your control's visibility you can use the following code snippets:
DockpaneVisibility = Visibility.Collapsed;
DockpaneVisibility = Visibility.Visible;
Finally, and you see the code for this also in the ViewModel file: SymbolLookupDockpaneViewModel.cs: you can subscribe to the ActiveMapViewChangedEvent either in the constructor on in InitializeAsync:
protected SymbolLookupDockpaneViewModel()
{
ActiveMapViewChangedEvent.Subscribe(OnActiveMapViewChangedEvent);
}
protected override async Task InitializeAsync()
{
ActiveMapViewChangedEvent.Subscribe(OnActiveMapViewChangedEvent);
}
And your subscription logic could look like this (similar to the sample I found):
private void OnActiveMapViewChangedEvent(ActiveMapViewChangedEventArgs obj)
{
if (obj.IncomingView == null)
{
DockpaneVisibility = Visibility.Collapsed;
return;
}
DockpaneVisibility = Visibility.Visible;
}
Since this code will only kick in after your dockpane is instantiated (it's constructor is called), you need to set up the initial visibility state in the constructor:
protected SymbolLookupDockpaneViewModel()
{
DockpaneVisibility = MapView.Active != null ? Visibility.Visible : Visibility.Collapsed;
ActiveMapViewChangedEvent.Subscribe(OnActiveMapViewChangedEvent);
}
I hope this helps.