【发布时间】:2014-08-08 15:29:15
【问题描述】:
情况如下:
程序集Common 实现CommonMessageBox.xaml 窗口,ContentControl 绑定到其DataContext。程序集AnotherAssembly 实现了MyViewModel 类和一个ResourceDictionary,其中一些DataTemplate 用于MyViewModel。它还引用了Common 程序集。我想显示CommonMessageBox 窗口并将MyViewModel 类型的对象分配给它的DataContext。
问题是:(如何)我可以(优雅且最好在 XAML 中)注入 ResourceDictionary 从 AnotherAssembly 到 CommonMessageBox 窗口的资源 Common 装配或App.xaml?
这个解决方案有效,但我想知道是否有另一种/更简单/更优雅的方式?
CommonMessageBox w = new CommonMessageBox();
w.DataContext = new MyViewModel();
w.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(@"/AnotherAssembly;component/DataTemplateDictionary.xaml", UriKind.RelativeOrAbsolute) });
w.ShowDialog();
【问题讨论】:
标签: c# wpf xaml .net-4.0 resourcedictionary