I need to create a simple CRUD, and i want to reuse the same "template" in different pages.
MyCrud.xaml (the template)
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- disegno la mia crud-->
<DataTemplate x:Key="MyCrud">
<StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,20,0,20">
<TextBlock Text="Code" FontWeight="Bold" Width="150" FontSize="24"/>
<TextBox x:Name="edtCode" InputScope="Number" Width="300" Margin="20,0,20,0" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,20,0,20">
<TextBlock Text="Vin" FontWeight="Bold" Width="150" FontSize="24"/>
<TextBox x:Name="edtVin" Width="300" Margin="20,0,20,0" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,20,0,20">
<TextBlock Text="Url" FontWeight="Bold" Width="150" FontSize="24"/>
<TextBox x:Name="edtUrl" Width="300" Margin="20,0,20,0" HorizontalAlignment="Left"/>
</StackPanel>
</StackPanel>
</DataTemplate>
Then I registered it in my App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="View/MyCrud.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
So now I want to put it in a page, and use it in my SearchPage.xaml
SearchPage.xaml
<Page ...bla bla bla>
<ContentControl x:Name="MyTemplate" Content="{Binding}" ContentTemplate="{StaticResource MyCrud}" />
</Page>
At the moment the Layout works perfectly
The questions are:
- How can I access the
TextBox
? For example the one callededtVin
from codebehind of SearchPage. - Why is
TextBox p= GetTemplateChild("edtVin") as TextBox;
always null? - I found this solution, is it the best one?
Thanks!