4

How is inheritance accomplished in XAML?

Is it true that only the code-behind can be inherited and not the .xaml (related question)? Is including the parent in the child control's namespace the only way of doing it?

There seems to be no general "inheritance" per se in XAML. There are questions about inheriting UserControl, etc. but not general inheritance.

Problem:

I have 2 xaml files, File1.xaml and File2.xaml, which are very alike. Can I create a SuperFile.xaml and put the bulk of the following code in it?

<UserControl ... >
    <Grid>
        <Grid.RowDefinitions ... />

        <DockPanel ... >
            <ToolBar ... >
                <!-- Some Buttons here:
                     File1.xaml to contain the Buttons "View" and "Export"
                     File2.xaml to contain the Buttons "Edit" and "Delete"
                 -->
            </ToolBar>
        </DockPanel>

        <igDP:XamDataGrid MouseDoubleClick="xamDataGrid1_MouseDoubleClick" ... />
    </Grid>
</UserControl>

The only things that differ in File1.xaml and File2.xaml are:

  1. Buttons in the ToolBar (see comment in the code)
  2. The properties of XamDataGrid, primarily the events (what they do in each case).

How can I achieve this? Would I have to write the code-behind files for both the children separately?

Community
  • 1
  • 1
mauryat
  • 1,610
  • 5
  • 29
  • 53
  • Do you truly need inheritance, or simply reuse the same class and have a flag that signals which mode you're in? – Joel Lucsy Jul 05 '12 at 15:52
  • @JoelLucsy I could do that if it was only the "eventing" that differed for both the files. How would I be able to have different `Button`s if I used the same _xaml_? Can a **flag** be used even in a _xaml_? – mauryat Jul 05 '12 at 16:11
  • Sure, bind the button's Visibility property to the button. You'd have to use a converter to change from bool to the Visibility enum, but there are plenty of examples of that out there. – Joel Lucsy Jul 05 '12 at 18:13
  • @JoelLucsy I guess your suggestion would have worked in this case, but I'm going by the `ResourceDictionary` way because it'll help me understand how to use it (at least for a future use-case). Thanks! – mauryat Jul 06 '12 at 22:18

1 Answers1

5

You can use a ResourceDictionary

Put all your generic templates and styles in a ResourceDictionary, then have both your UserControls import that ResourceDictionary

<UserControl.Resources>
    <ResourceDictionary Source="BaseStylesAndTemplates.xaml" />
</UserControl.Resources>
Rachel
  • 130,264
  • 66
  • 304
  • 490
  • Thanks! By following this approach, will I be able to embed different sets of `Button`s in the 2 child _XAMLs_? For instance, `File1.xaml` should have the `Button`s "View" and "Export" within the `ToolBar` and `File2.xaml` should have the `Button`s "Edit" and "Delete". See the edit in the question. – mauryat Jul 05 '12 at 19:23
  • 1
    @maelstrom3 I don't see why you wouldn't be able to. Your base styles will all be pulled from the `ResourceDictionary`, while your individual components such as the buttons will be defined in your individual `UserControls` – Rachel Jul 05 '12 at 19:39
  • I'm not so experienced in _WPF_ and hence haven't used _Styles_ yet. So, I'll take your word for it :) and try it out. – mauryat Jul 05 '12 at 19:42
  • I got the thing partially working. I have a `DataTemplate` in *SuperFile.xaml* which I'm applying to a `ContentControl` in *File1.xaml*. But, how can add `Button`s to the `DataTemplate`-generated element `ToolBar`? See the code-snippet in the question for how *SuperFile.xaml* looks like. – mauryat Jul 11 '12 at 18:21