1

Using Caliburn Micro 1.5.1 I'm trying to get design time bindings to work in a WP8 app. I have created a design time ViewModel which I specify explicitly in the PhoneApplicationPage:

<phone:PhoneApplicationPage
    d:DataContext="{Binding Source={d:DesignInstance Type=designTime:StartPageDesignTimeViewModel, IsDesignTimeCreatable=True}}"
    micro:Bind.AtDesignTime="True"

The page is really no more than a RadDataBoundListBox from Telerik:

<Grid x:Name="ContentPanel">
    <telerikPrimitives:RadDataBoundListBox x:Name="Rooms"  ...>

As you can see, my ViewModel (and design time view model) have a public property named Rooms which I am binding to the ItemsSource collection using the named convention approach. The approach doesn't work at design time, however, unless I add the ItemsSource property

<Grid x:Name="ContentPanel">
    <telerikPrimitives:RadDataBoundListBox x:Name="Rooms" ItemsSource="{Binding Rooms}" ...>

However, when I use ItemsSource binding I lose the CM wire-up magic like SelectedItem. Is there a way to get my bindings to work at design time using the naming conventions approach without modifying the page with anything other than design time attributes?

4

1 回答 1

2

好的,我想通了。我一直在寻找的是始终覆盖现有绑定的能力。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;
};

我强制框架始终替换绑定的做法是将调用替换为在我的 BootStrapper 中SetBindingWithoutBindingOrValueOverwrite直接调用。SetBinding所以:

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;
                             };

(我还必须对我之前为 RadDataBoundListBox 添加的约定进行此编辑)

我可以看到在某些情况下,有人可能想要以声明方式强制替换现有绑定。也许我会写一个补丁...

于 2013-05-03T05:57:29.603 回答