C# 使用命名约定的Caliburn微设计时间
使用Caliburn Micro 1.5.1,我试图让设计时绑定在WP8应用程序中工作。我创建了一个设计时视图模型,我在PhoneApplicationPage中明确指定了该模型:C# 使用命名约定的Caliburn微设计时间,c#,data-binding,caliburn.micro,design-time,design-time-data,C#,Data Binding,Caliburn.micro,Design Time,Design Time Data,使用Caliburn Micro 1.5.1,我试图让设计时绑定在WP8应用程序中工作。我创建了一个设计时视图模型,我在PhoneApplicationPage中明确指定了该模型: <phone:PhoneApplicationPage d:DataContext="{Binding Source={d:DesignInstance Type=designTime:StartPageDesignTimeViewModel, IsDesignTimeCreatable=True}}"
<phone:PhoneApplicationPage
d:DataContext="{Binding Source={d:DesignInstance Type=designTime:StartPageDesignTimeViewModel, IsDesignTimeCreatable=True}}"
micro:Bind.AtDesignTime="True"
好的,我想出来了。我想要的是能够随时覆盖现有绑定。CM更具防御性,因此默认情况下,它不会替换ItemsControl的现有绑定或值。ConventionManager.cs中定义了此行为:
AddElementConvention<ItemsControl>(ItemsControl.ItemsSourceProperty, "DataContext", "Loaded")
.ApplyBinding = (viewModelType, path, property, element, convention) => {
if (!SetBindingWithoutBindingOrValueOverwrite(viewModelType, path, property, element, convention, ItemsControl.ItemsSourceProperty)) {
return false;
}
ApplyItemTemplate((ItemsControl)element, property);
return true;
};
(我还必须对我先前为RadDataBoundListBox添加的约定进行编辑)
在某些情况下,我可以看到有人可能希望声明性地强制替换现有绑定。也许我会写一个补丁
<Grid x:Name="ContentPanel">
<telerikPrimitives:RadDataBoundListBox x:Name="Rooms" ItemsSource="{Binding Rooms}" ...>
AddElementConvention<ItemsControl>(ItemsControl.ItemsSourceProperty, "DataContext", "Loaded")
.ApplyBinding = (viewModelType, path, property, element, convention) => {
if (!SetBindingWithoutBindingOrValueOverwrite(viewModelType, path, property, element, convention, ItemsControl.ItemsSourceProperty)) {
return false;
}
ApplyItemTemplate((ItemsControl)element, property);
return true;
};
ConventionManager.AddElementConvention<ItemsControl>(ItemsControl.ItemsSourceProperty, "DataContext", "Loaded")
.ApplyBinding = (viewModelType, path, property, element, convention) => {
ConventionManager.SetBinding(viewModelType, path, property, element, convention, ItemsControl.ItemsSourceProperty);
ConventionManager.ApplyItemTemplate((ItemsControl) element, property);
return true;
};