根据属性值动态加载ResourceDictionary项目

根据属性值动态加载ResourceDictionary项目

问题描述:

是否可以根据我的viewmodel中的字符串属性加载我的一个堆栈面板?所以如果字符串是MyStackPanel1,那么合适的堆栈面板将被注入到我的主窗口的网格中。根据属性值动态加载ResourceDictionary项目

我的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> 

我的主窗口:

<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> 

这里的视图模型的想法:

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

    private void ChangedHandler(string propertyToBeChanged) { 

    } 
} 
+1

而不是资源字典存储''的(警告:他们共享),我建议您切换到DataTemplates并使用DataTemplateSelector根据属性值选择正确的模板(或者不带DataTemplateSelector并通过Style DataTrigger设置选择正确的模板) – ASh

+0

在这里看到一些示例:https://*.com/questions/20468126/contentcontrol-contenttemplateselector-dynamically - 选择模板 – ASh

+0

@只读https://msdn.microsoft.com/d e-de/library/system.windows.controls.datatemplateselector(v = vs.110).aspx – Asperger

你可以使用与ContentTemplates一个ContentControl但对于绑定工作,你应该设置ContentControlContent属性:

<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> 

我想我有一个想法如何解决这个问题。首先,我定义的资源列表:

在XAML我写:

<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> 

现在在我的资源字典:

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

// Resource2 and so on 

然后我认为我可以做到以下几点:

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

问题是,绑定不会工作...