J
JennyLiu001
Guest
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"
designHeight="300" designWidth="300"
Title="Page1">
<Grid>
<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"/>
<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"
designHeight="300" designWidth="300"
Title="Page2">
<Grid>
<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"/>
<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"
designHeight="300" designWidth="300"
Title="Page3">
<Grid>
<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"/>
<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.cs
using 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));
}
}
}
Continue reading...
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"
designHeight="300" designWidth="300"
Title="Page1">
<Grid>
<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"/>
<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"
designHeight="300" designWidth="300"
Title="Page2">
<Grid>
<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"/>
<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"
designHeight="300" designWidth="300"
Title="Page3">
<Grid>
<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"/>
<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.cs
using 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));
}
}
}
Continue reading...