2017-08-15 9 views
0

私の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> 
ここ

のviewmodelのアイデア:

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

    private void ChangedHandler(string propertyToBeChanged) { 

    } 
} 
+1

を代わりにDataTemplateに渡し、DataTemplateSelectorを使用してプロパティ値に基づいて正しいテンプレートを選択する(またはDataTemplateSelectorを使用せずに、Style DataTriggerで正しいテンプレートを選択する) – ASh

+0

ここでいくつかの例を参照してください:https://stackoverflow.com/questions/20468126/contentcontrol-contenttemplateselector-dynamically -select-template – ASh

+0

@ASh iveただhttps://msdn.microsoft.com/dを読んでくださいe-de/library/system.windows.controls.datatemplateselector(v = vs.110).aspx – Asperger

答えて

1

あなたはContentTemplatesContentControlを使用することができますが、バインディングが動作するために、あなたはContentControlContentプロパティを設定する必要があります私はあなたが切り替わり勧め:(彼らは共有している警告)のResourceDictionaryに `` を格納する

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

私はこの問題を解決する方法のアイデアを持っていると思います。まず私は、リソースのリストを定義します。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); 
} 

問題は、バインディングが機能しないことです。

関連する問題