Is it possible for WPF pages to share XAML snippets?

jennyliu835 221 Reputation points
2020-08-19T18:42:05.6+00:00

Hi Microsoft Geniuses,

The attached 8 files indicate the specific scenario for my question. Please remove the ".txt" extension at the end of each file name and add "." before the left extensions.

Thanks

18719-page1xaml.txt18911-page1xamlcs.txt18894-page2xaml.txt18808-page2xamlcs.txt18865-page3xaml.txt18809-page3xamlcs.txt18882-mainwindowxaml.txt18902-mainwindowxamlcs.txt

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,784 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,326 Reputation points
    2020-08-21T04:12:22.817+00:00

    Hi Jenny,
    to solve your problem you can use Binding instead of assign in CodeBehind.

    • in page use ContentControl bound to property in DataContext <Grid x:Name="grd" HorizontalAlignment="Center">
      <!-- common XAML to be shared -->
      <ContentControl Content="{Binding LoadedXaml}"/>
      <!-- end common XAML to be shared -->
      <TextBlock FontSize="24" TextWrapping="Wrap"
      Text="HTML" HorizontalAlignment="Center" Margin="114,80,114,140"/>
      <TextBlock HorizontalAlignment="Left" Margin="60,115,0,0" Visibility="{Binding TbExplainVisibility}"
      TextWrapping="Wrap" Text="HTML is..." VerticalAlignment="Top" Height="26" Width="148"/>
      </Grid>
    • for complex XAML use UserControl (in file) with Command-Binding and Interactivity.dll:

    19353-x.png

    • and in CodeBehind include properties for Binding and use CodeBehind as DataContext:

    19190-x.png

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. DaisyTian-1203 11,626 Reputation points
    2020-08-20T03:23:14.517+00:00

    The code in my XamlWithControls.xaml is :
    18965-capture.png

    1.If you want to get the Button's content[Button from XamlWithControls], you can add a ContentControl in the Page.xaml and bind the data like below:

            <ContentControl Content="{Binding LoadedXaml}"></ContentControl>  
    

    2.If you want get the all xaml code as the picture shown, add the below code:

     public string LoadedXamlCode  
            {  
                get => XamlWriter.Save((XamlReader.Load(new FileStream("XamlWithControls.xaml", FileMode.Open))));  
            }  
    

    Then bind the data to the ContentControl like:

     <ContentControl Content="{Binding LoadedXamlCode}"></ContentControl>  
    

    3.If you want to show the Button in the Page1, you can name your gird as MyGrid and add the below code in the cs:
    19011-capture2.png

    1 person found this answer helpful.

  2. Ken Tucker 5,856 Reputation points
    2020-08-19T23:59:58.233+00:00
    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.