What is the proper way binding from XAML to the Model in ArcGIS Pro SDK framework

5140
10
Jump to solution
11-09-2020 05:50 PM
Amadeus111
Occasional Contributor II

I am able to access properties of an XAML control items directly. For instance, changing a header of a tab in  a Dockpane

XAML (View):

<TabControl x:Name="FavoritesTabs" HorizontalAlignment="Stretch" Height="23" Initialized="FavoritesTabs_Initialized" Margin="8,0,7,0" >
<TabItem x:Name="Tab1" Height="20" Width="75"> </TabItem>
 <TabItem x:Name="Tab2" Height="20" Width="75"></TabItem>
</TabControl>

C# (Model):

private void FavoritesTabs_Initialized(object sender, EventArgs e)
{
    Tab1.Header = "MyFavoriteTab";
}

However, I know it this is not right/proper way and I would like to use MVVM. I would like to bind tab Headers to Model by using MVVM. How can I achieve that?

I think XAML would be 

XAML:

<TabControl x:Name="FavoritesTabs" HorizontalAlignment="Stretch" Height="23" Initialized="FavoritesTabs_Initialized" Margin="8,0,7,0" >
<TabItem x:Name="Tab1" Header="{Binding MyFavorite1}" Height="20" Width="75"> </TabItem>
 <TabItem x:Name="Tab2" Header="{Binding MyFavorite2}" Height="20" Width="75"></TabItem>
</TabControl>


What else I should add Dockpane1ViewModel.cs (View Model) and Dockpane1.xaml.cs (Model)?

0 Kudos
10 Replies
GKmieliauskas
Esri Regular Contributor

In MVVM you need to override Docpane's OnActivate  or InitializeAsync to fill your observable collection like this:

protected override void OnActivate(bool isActive)
{
base.OnActivate(isActive);

if (isActive)
{
// Place here your observable collection initialization if it depends on map situation or etc.
}
}

protected override async Task InitializeAsync()
{
await base.InitializeAsync();

// Place here your observable collection initialization if it needs one time creation
}