duplicate page

Eduardo Gomez 3,651 Reputation points
2022-10-23T00:28:52.447+00:00

I have my MainWindows, with a sidebar and a frame.

I have several pages, and many of them look many, except for the title

My question is How can I make b.XAML inherit from my a.XAML so I don't have to write the same XAML

For example, I have this XAML in my "a" page

xmlns:res="clr-namespace:TranscribeMe.Resources"
xmlns:ui="http://schemas.modernwpf.com/2019"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">

<Grid Margin="20">  
    <Grid.ColumnDefinitions>  
        <ColumnDefinition Width="Auto" />  
        <ColumnDefinition />  
    </Grid.ColumnDefinitions>  
    <Grid.RowDefinitions>  
        <RowDefinition />  
        <RowDefinition />  
        <RowDefinition />  
        <RowDefinition />  
        <RowDefinition />  
        <RowDefinition />  
    </Grid.RowDefinitions>  

    <Label // This one Is the title of the page  
        HorizontalAlignment="Center"  
        Grid.ColumnSpan="2"  
        Content="{x:Static res:Lang.AudioService}"  
        FontSize="18"  
        FontWeight="Bold" />  

    <Button  
        VerticalAlignment="Top"  
        Grid.Row="1"  
        Command="{Binding PickFileCommad}"  
        Content="Pick a file"  
        Focusable="False"  
        FontWeight="Bold" />  

    <Label  
        Grid.Row="2"  
        Margin="0,5,0,0"  
        VerticalContentAlignment="Top"  
        Content="{x:Static res:Lang.FileLang}" />  

    <TextBox  
        Grid.Column="1"  
        Grid.Row="1"  
        Margin="10,0,10,0"  
        VerticalAlignment="Top"  
        IsReadOnly="True"  
        Text="{Binding FilePath}" />  

    <ComboBox  
        x:Name="combo"  
        Grid.Row="2"  
        Grid.Column="1"  
        Margin="10,0,10,0"  
        HorizontalAlignment="Stretch"  
        ui:ControlHelper.PlaceholderText="Select Lenguague"  
        DisplayMemberPath="Value.Name"  
        ItemsSource="{Binding LanguagesDictionary}"  
        SelectedValue="{Binding SelectedItem}"  
        SelectedValuePath="Value.Code" />  

    <Label  
        Grid.Row="3"  
        Grid.ColumnSpan="2"  
        Margin="10,0,10,0"  
        HorizontalContentAlignment="Center"  
        VerticalContentAlignment="Center"  
        Content="{x:Static res:Lang.ProcessMsg}"  
        Visibility="{Binding CanShow}" />  

    <ui:ProgressRing  
        Grid.Row="4"  
        Grid.ColumnSpan="2"  
        Width="60"  
        Height="60"  
        Margin="10,0,10,0"  
        IsActive="{Binding IsBusy}" />  

    <Button  
        Grid.Row="6"  
        Grid.ColumnSpan="2"  
        Margin="10,0,10,10"  
        HorizontalAlignment="Stretch"  
        VerticalAlignment="Bottom"  
        Command="{Binding StartCommand}"  
        Content="Start"  
        Focusable="False" />  
</Grid>  

How can I make my "B" page inherit from "a"

Developer technologies XAML
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2022-10-24T03:09:19.377+00:00

    WPF does not allow you to visually inherit in this way.
    The reason WPF does not support visual inheritance is due to the fact that the form design is achieved via markup (.xaml) as opposed to winforms which use code-behind to produce the layout. Code-behind is extensible and markup is not.

    I have several pages, and many of them look many, except for the title

    If the above means that many of them look the same except for the title. If you want to set different styles of Label, you could set different styles in App.xaml and then bind them to the corresponding Label of Page.
    Page1:

       <Label   
             HorizontalAlignment="Center"  
             Grid.ColumnSpan="2"  
             Content="page1"  
             FontSize="18"  
             FontWeight="Bold" Style="{Binding Source={StaticResource LabelPage1}}" />  
    

    Page2:

       <Label   
             HorizontalAlignment="Center"  
             Grid.ColumnSpan="2"  
             Content="page2"  
             FontSize="18"  
             FontWeight="Bold" Style="{Binding Source={StaticResource LabelPage2}}" />  
    

    App.xaml:

    253339-image.png

    Update : example

    MyView.xaml:(UserControl)

     <Grid>  
            <ComboBox ItemsSource="{Binding TestData}" />  
        </Grid>  
    

    Dictionary1.xaml:(ResourceDictionary)

     <DataTemplate DataType="{x:Type local:MyViewModel}">  
            <local:MyView/>  
        </DataTemplate>  
    

    App.xaml:

    <Application x:Class="XamlViewInherite.App"  
    ...  
                 xmlns:local="clr-namespace:XamlViewInherite"  
                 StartupUri="MainWindow.xaml">  
        <Application.Resources>  
            <ResourceDictionary Source="Dictionary1.xaml" />  
        </Application.Resources>  
    </Application>  
    

    Page1.xaml:

    253770-image.png

    Page2.xaml:

    253872-image.png

    MainWindow.xaml:

    253873-image.png
    MyViewModel:

    253778-viewmodel.txt

    The result:

    253881-image.png

    ----------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

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.