How to include common XAML code in page?

jennyliu835 221 Reputation points
2020-08-17T22:35:01.94+00:00

Hi Geniuses,

How to let WPF pages share common info or XAML code?

5 files are attached. Please remove the ".txt" extension at the end of each file name and add "." before the left extensions.
18075-page1xaml.txt
18067-page2xaml.txt
18035-page3xaml.txt
18133-mainwindowxaml.txt
18113-mainwindowxamlcs.txt

Thanks

Jenny

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,785 questions
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,326 Reputation points
    2020-08-18T06:22:46.513+00:00

    Hi Jenny,
    use ContentControl to show loaded xaml:

    18175-xpage1.png

    And CodeBehind:

    18243-xpage1.png

    XAML content:

    <Label xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Content="This is text for Page 1"/>  
    

    Result:

    18244-xpage1.png

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. DaisyTian-1203 11,626 Reputation points
    2020-08-18T05:52:33.547+00:00

    You can set the event loaded for the TextBlock, in my way, I add Loaded="Text_Loaded" for the TextBlock in the Page1.xaml and add the code in it's cs as:

     private void Text_Loaded(object sender, RoutedEventArgs e)  
            {  
                string savedAsString = XamlWriter.Save(XamlReader.Load(new FileStream("MyXamlWindow.xaml", FileMode.Open)));  
                (sender as TextBlock).Text = savedAsString;   
            }  
    

    The result picture is:
    18197-capture.png

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.