Answered by:
How to include common XAML code in page?

Question
-
Hi Geniuses,
How to let WPF pages share common info or XAML code?
Here is my code.
Page1
<Page x:Class="WpfApp1.pages.Page1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WpfApp1.pages" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" Title="Page1"> <Grid> <!-- How to replace the following code with an external xaml file? thanks --> <TextBlock Width="{Binding ActualWidth, ElementName=fm}" FontSize="20" TextWrapping="Wrap" Text="common info (i.e. xaml code)" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,50,0,0"/> <!-- end How to --> <TextBlock Width="{Binding ActualWidth, ElementName=fm}" FontSize="36" TextWrapping="Wrap" Text="Page 1" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </Page>
Page2
<Page x:Class="WpfApp1.pages.Page2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WpfApp1.pages" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" Title="Page2"> <Grid> <!-- How to replace the following code with an external xaml file? thanks --> <TextBlock Width="{Binding ActualWidth, ElementName=fm}" FontSize="20" TextWrapping="Wrap" Text="common info (i.e. xaml code)" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,50,0,0"/> <!-- end How to --> <TextBlock Width="{Binding ActualWidth, ElementName=fm}" FontSize="36" TextWrapping="Wrap" Text="Page 2" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </Page>
Page3
<Page x:Class="WpfApp1.pages.Page3" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WpfApp1.pages" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" Title="Page3"> <Grid> <!-- How to replace the following code with an external xaml file? thanks --> <TextBlock Width="{Binding ActualWidth, ElementName=fm}" FontSize="20" TextWrapping="Wrap" Text="common info (i.e. xaml code)" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,50,0,0"/> <!-- end How to --> <TextBlock Width="{Binding ActualWidth, ElementName=fm}" FontSize="36" TextWrapping="Wrap" Text="Page 3" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </Page>
MainWindow:
<Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <StackPanel Orientation="Horizontal" Height="30" Margin="0" VerticalAlignment="Top" > <Button Content="Page 1" Width="80" Click="BtnClickP1" /> <Button Content="Page 2" Width="80" Margin="10,0,0,0" Click="BtnClickP2"/> <Button Content="Page 3" Width="80" Margin="10,0,0,0" Click="BtnClickP3"/> </StackPanel> <Frame x:Name="fm" Margin="0,40,0,0" /> </Grid> </Window>
MainWindow.xaml.csusing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApp1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void BtnClickP1(object sender, RoutedEventArgs e) { NavigationService NS = fm.NavigationService; NS.Navigate(new Uri("pages/Page1.xaml", UriKind.Relative)); } private void BtnClickP2(object sender, RoutedEventArgs e) { NavigationService NS = fm.NavigationService; NS.Navigate(new Uri("pages/Page2.xaml", UriKind.Relative)); } private void BtnClickP3(object sender, RoutedEventArgs e) { NavigationService NS = fm.NavigationService; NS.Navigate(new Uri("pages/Page3.xaml", UriKind.Relative)); } } }
- Moved by Dave PatrickMVP Monday, August 17, 2020 6:56 PM looking for forum
Monday, August 17, 2020 6:52 PM
Answers
-
Hi, Dave.
I tried to post my question at docs.microsoft.com/en-us/answers/topics/windows-wpf.html
But I got following message:
"You don't have permission to access "http://docs.microsoft.com/answers/questions/ask.html" on this server.
Reference #18.30be2817.1597693914.18aa06b8"
So where should I post my question?
Thanks,
Jenny
Good day Jenny,
This error is a bug in the system which was fixed and came back. It is not something related to your specific user. I am a Moderator in the QnA system and I got this message several days ago :-(
It is probably related to the content of your message (not a problematic content but a problem with the system which see legit content as an issue).
You have several options:
1. try to re-ask the question in a different way. Maybe post it first in short version and after it was well published you can edit the content - this works in some cases
2. Post a short question and attach a file with the full question
Note! This mean that most people will probably not going to check your file since they (including myself) do not use files from un-known source :-)
If you can use simple text file then the chance others will check it is higher
Ronen Ariely
[Personal Site] [Blog] [Facebook] [Linkedin]- Proposed as answer by Dave PatrickMVP Friday, August 21, 2020 12:53 PM
- Marked as answer by Dave PatrickMVP Saturday, August 22, 2020 10:11 PM
Wednesday, August 19, 2020 6:18 AM -
I'd try asking for help over here.
https://docs.microsoft.com/en-us/answers/topics/windows-wpf.html
Regards, Dave Patrick ....
Microsoft Certified Professional
Microsoft MVP [Windows Server] Datacenter Management
Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.- Proposed as answer by Dave PatrickMVP Friday, August 21, 2020 12:53 PM
- Marked as answer by Dave PatrickMVP Saturday, August 22, 2020 10:11 PM
Monday, August 17, 2020 6:55 PM
All replies
-
I'd try asking for help over here.
https://docs.microsoft.com/en-us/answers/topics/windows-wpf.html
Regards, Dave Patrick ....
Microsoft Certified Professional
Microsoft MVP [Windows Server] Datacenter Management
Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.- Proposed as answer by Dave PatrickMVP Friday, August 21, 2020 12:53 PM
- Marked as answer by Dave PatrickMVP Saturday, August 22, 2020 10:11 PM
Monday, August 17, 2020 6:55 PM -
Hi, Dave.
I tried to post my question at docs.microsoft.com/en-us/answers/topics/windows-wpf.html
But I got following message:
"You don't have permission to access "http://docs.microsoft.com/answers/questions/ask.html" on this server.
Reference #18.30be2817.1597693914.18aa06b8"
So where should I post my question?
Thanks,
Jenny
Monday, August 17, 2020 7:56 PM -
May be related to the content.
Regards, Dave Patrick ....
Microsoft Certified Professional
Microsoft MVP [Windows Server] Datacenter Management
Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.Monday, August 17, 2020 8:56 PM -
Hi, Dave.
I tried to post my question at docs.microsoft.com/en-us/answers/topics/windows-wpf.html
But I got following message:
"You don't have permission to access "http://docs.microsoft.com/answers/questions/ask.html" on this server.
Reference #18.30be2817.1597693914.18aa06b8"
So where should I post my question?
Thanks,
Jenny
Good day Jenny,
This error is a bug in the system which was fixed and came back. It is not something related to your specific user. I am a Moderator in the QnA system and I got this message several days ago :-(
It is probably related to the content of your message (not a problematic content but a problem with the system which see legit content as an issue).
You have several options:
1. try to re-ask the question in a different way. Maybe post it first in short version and after it was well published you can edit the content - this works in some cases
2. Post a short question and attach a file with the full question
Note! This mean that most people will probably not going to check your file since they (including myself) do not use files from un-known source :-)
If you can use simple text file then the chance others will check it is higher
Ronen Ariely
[Personal Site] [Blog] [Facebook] [Linkedin]- Proposed as answer by Dave PatrickMVP Friday, August 21, 2020 12:53 PM
- Marked as answer by Dave PatrickMVP Saturday, August 22, 2020 10:11 PM
Wednesday, August 19, 2020 6:18 AM