Reputation: 36
I write a vector editor. Before I started using TabControl, everything worked for me. However, it became necessary to upload several open files simultaneously. Each open file is displayed on TabItem. As a result of chatting on forums, I found out that you need to create an MVVM to correctly display data from different different files. I was advised to Set a common template for all tabs. By adding items to the collection that TabControl is linked to, tabs will be created automatically using this template. I did the following:
<TabControl x:Name="Drawing_TabControl" Grid.Row="0" Background="WhiteSmoke"
SelectionChanged="Drawing_TabControl_SelectionChanged">
<TabControl.ContentTemplate>
<DataTemplate>
<Grid x:Name="Drawing_Grid_Tab"
Background="WhiteSmoke">
<Canvas x:Name="coordinateCanvas_Tab"
Background="GhostWhite"
Height="6cm"
Width="16cm"
HorizontalAlignment="Center"
VerticalAlignment="Center"></Canvas>
<Canvas x:Name="gridCanvas_Tab"
Background="Transparent"
Height="6cm"
Width="16cm"
HorizontalAlignment="Center"
VerticalAlignment="Center"
SizeChanged="gridCanvas_SizeChanged"></Canvas>
<Border x:Name="drawing_Border_Tab"
BorderBrush="Black"
BorderThickness="0.5 0.5 0.5 0.5"
Height="4cm"
Width="14cm"
HorizontalAlignment="Center"
VerticalAlignment="Center"></Border>
<Canvas x:Name="drawing_gridCanvas_Tab"
Background="White"
Height="4cm"
Width="14cm"
HorizontalAlignment="Center"
VerticalAlignment="Center"></Canvas>
<Canvas x:Name="drawing_tempCanvas_Tab"
Background="Transparent"
Height="4cm"
Width="14cm"
HorizontalAlignment="Center"
VerticalAlignment="Center"></Canvas>
<Canvas x:Name="drawingCanvas_Tab"
Background="Transparent"
Height="4cm"
Width="14cm"
HorizontalAlignment="Center"
VerticalAlignment="Center"
ClipToBounds="True"></Canvas>
</Grid>
</DataTemplate>
</TabControl.ContentTemplate>
<TabItem x:Name="drawTabItem1"
Header="Макет_1" Background="WhiteSmoke">
</TabItem>
</TabControl>[enter image description here][1]
How can I access x:Name="coordinateCanvas_Tab"
in the back code, for example?
Upvotes: 0
Views: 343
Reputation: 66
If you've been advised and have implement that advice to change to an MVVM code structure then you do not access the XAML component directly from the code behind file anymore. Instead you need to wire up a two way binding between your View XAML and the ViewModel you are using.
This is primarily done by utilising the BindingContext object in the view after the screen is initialized in your constructor code behind.
After that you can then assign values to a Collection object in your ViewModel and bind to it in the XAML. To do the dynamic tab insertion you want to do you can make use of the BindableLayout attribute then in the ItemTemplate bind values for each tab from the collection objects.
Have a look at this guide for a better understanding of Bindings.
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/data-bindings-to-mvvm
Upvotes: 0