【发布时间】:2015-02-25 09:34:40
【问题描述】:
我习惯于使用 MVVM-light 定位器将 xaml 中的 UserControl 的数据上下文设置为视图模型。例如:
DataContext="{Binding SplashMainViewModel, Mode=OneWay, Source={StaticResource Locator}}"
这是在 xaml 中的视图标记中完成的。
如何将 CustomControl 的数据上下文设置为视图模型?在 VS 中,自定义控件是使用以下内容创建的:
public class CustomControl1 : Control
{
static CustomControl1()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1),new FrameworkPropertyMetadata(typeof(CustomControl1)));
}
}
但它没有通常的 xaml 标记。
【问题讨论】:
-
你能把你的问题说清楚吗?要将
CustomViewModel设置为CustomControl? -
自定义控件通常在
Themes\Generic.xaml中有一个样式。您也许可以在那里设置 DataContext (假设 Locator 资源在那里可用)。否则,您应该能够在控件的构造函数中设置它。或者只是不要全部设置,让其在控件实例化时被继承或显式设置。
标签: c# wpf mvvm-light