私の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) {
}
}
を代わりにDataTemplateに渡し、DataTemplateSelectorを使用してプロパティ値に基づいて正しいテンプレートを選択する(またはDataTemplateSelectorを使用せずに、Style DataTriggerで正しいテンプレートを選択する) – ASh
ここでいくつかの例を参照してください:https://stackoverflow.com/questions/20468126/contentcontrol-contenttemplateselector-dynamically -select-template – ASh
@ASh iveただhttps://msdn.microsoft.com/dを読んでくださいe-de/library/system.windows.controls.datatemplateselector(v = vs.110).aspx – Asperger