Asperger
Asperger

Reputation: 3222

Load ResourceDictionary item dynamically based on property value

Is it possible to load one of my stackpanels based on a string property inside my viewmodel? So if string is MyStackPanel1 then the appropiate stackpanel will be injected into the grid of my mainwindow.

My ResourceDictionary

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">


    <StackPanel x:Key="MyStackPanel1" Background="{Binding Color}"> 
       // Has some content     
    </StackPanel>

    <StackPanel x:Key="MyStackPanel2" Background="{Binding Color}">   
     // Has some other content
    </StackPanel>
</ResourceDictionary>

My MainWindow:

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <Grid>

    </Grid>
</Window>

Here an idea of the viewmodel:

public class ViewModel : INotifyPropertyChanged {
  public event PropertyChangedEventHandler PropertyChanged;
  public string StackPanelName { get; set; };
  public string Color { get; set; };

   private void ChangedHandler(string propertyToBeChanged) {

   }
}

Upvotes: 0

Views: 623

Answers (2)

mm8
mm8

Reputation: 169400

You could use a ContentControl with ContentTemplates but for the bindings to work you should set the Content property of the ContentControl:

<Window.Resources>
    <ResourceDictionary>
        <DataTemplate x:Key="MyResource1" x:Shared="false">
            <StackPanel>
                <TextBlock Background="{Binding background}">Hello World</TextBlock>
            </StackPanel>
        </DataTemplate>

        <!-- Resource2 and so on -->
    </ResourceDictionary>
</Window.Resources>

<Grid x:Name="Body">
    <!-- "background" is a property of the view model -->
    <ContentControl x:Name="Sample" Content="{Binding}" ContentTemplate="{StaticResource MyResource1}"/>
</Grid>

Upvotes: 1

Asperger
Asperger

Reputation: 3222

I think I have an idea how to solve this. First I define a list of resources:

In XAML I write:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MyResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

<Grid x:Name="Body">
    <ContentControl x:Name="Sample" ContentTemplate="{StaticResource MyResource1}"/>
</Grid>

Now in my resource dictionary:

<DataTemplate x:Key="MyResource1" x:Shared="false">
    <StackPanel>
        <TextBlock Background="{Binding background}">Hello World</TextBlock>
    </StackPanel>
</DataTemplate>

// Resource2 and so on

Then In my view I can do the following:

public void SwapResource(ContentControl contentControl, string resourceName) {
    contentControl.ContentTemplate = (DataTemplate)FindResource(resourceName);
}

Problem is that the bindings wont work...

Upvotes: 1

Related Questions