I have a ViewModel-First MVVM WPF Project. I want to load a different FlowDocument.xaml file depending on which option in a ComboBox has been selected.
This is what I have tried so far:
In my XAML View I have added the following:
<UserControl.Resources>
<ResourceDictionary x:Key="ColebrookWhiteEquation">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Views\FlowDocuments\ColebrookWhiteEquation.xaml"></ResourceDictionary>
<ResourceDictionary Source="Views\FlowDocuments\ManningEquation.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
A cut down example of one of my FlowDocuments is as follows:
<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:Drain.Views.FlowDocuments"
xmlns:controls="clr-namespace:WpfMath.Controls;assembly=WpfMath"
ColumnWidth="400" FontSize="14" FontFamily="Georgia">
<Paragraph FontFamily="Calibri" FontWeight="Bold" FontSize="24">
Colebrook White Equation
</Paragraph>
...
My first issue is ResourceDictionary
can't be found (i.e. "Views\FlowDocuments\ColebrookWhiteEquation.xaml" for example). This is despite the FlowDocument xaml file being set as Resource and Copy always. I can also confirm the file is copying to the correct location following a build.
I then plan to add a series of FlowDocumentPageViewer
tags but this is giving me an error that ColebrookWhiteEquation is an incompatible file type. This may be due to the issue above. Can anyone confirm? Here is the XAML for it.
<FlowDocumentPageViewer Document="{StaticResource ColebrookWhiteEquation}"></FlowDocumentPageViewer>
If I can get the above working I plan to add the following to each FlowDocumentPageViewer
.
<Style TargetType="FlowDocumentPageViewer ">
<Setter Property="Visibility" Value="Visible"/>
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedFlowEquationType.ID}" Value="1">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
The only issue with the above solution as far as I can see is it does mean loading up all FlowDocuments regardless of whether they are needed or not. Provided the number of FlowDocuments is low I don't think this will be an issue.
If there is a simpler MVVM approach to this I would be grateful to hear it.
ColebrookWhiteEquation.xaml
should contain aResourceDictionary
root element that in turn contains aFlowDocument
resource with anx:Key
, e.g.:Only then you can reference the
FlowDocument
resource directly in the XAML markup like this:So the referenced XAML files should be resource dictionaries and the resources in these should be
FlowDocuments
.