4

我正在嘗試caliburn.micro框架爲一個新的項目,但我堅持綁定一個ListPicker(從工具包)。當我將控件更改爲簡單的DropDown時,一切都按預期工作。 我認爲下拉工作,因爲默認慣例的正確,實現here針對WP7上的ListPicker caliburn.micro綁定約定

AddElementConvention<Selector>(Selector.ItemsSourceProperty, "SelectedItem", "SelectionChanged") 
    .ApplyBinding = (viewModelType, path, property, element, convention) => { 
     if (!SetBinding(viewModelType, path, property, element, convention)) 
      return false; 

     ConfigureSelectedItem(element, Selector.SelectedItemProperty,viewModelType, path); 
     ApplyItemTemplate((ItemsControl)element, property); 

     return true; 
    }; 

的ListPicker沒有實現選擇,所以我想在我的引導程序添加自定義的約定:

static void AddCustomConventions() { 
    AddElementConvention<ListPicker>(ListPicker.ItemsSourceProperty, "SelectedItem", "SelectionChanged") 
     .ApplyBinding = (viewModelType, path, property, element, convention) => { 
      ConventionManager.ConfigureSelectedItem(element, ListPicker.SelectedItemProperty,viewModelType, path); 
      return true; 
     }; 
} 

不幸的是,這並不奏效。你能幫我嗎?

回答

8

我解決了這個約定的問題。

ConventionManager.AddElementConvention<ListPicker>(ListPicker.ItemsSourceProperty, "SelectedItem", "SelectionChanged") 
    .ApplyBinding = (viewModelType, path, property, element, convention) => 
    { 
     if (ConventionManager.GetElementConvention(typeof(ItemsControl)).ApplyBinding(viewModelType, path, property, element, convention)) 
     { 
      ConventionManager.ConfigureSelectedItem(element, ListPicker.SelectedItemProperty, viewModelType, path); 
      return true; 
     } 
     return false; 
    }; 

另外,還有另一個問題。我的SelectedItem屬性返回null,但我的項目屬性沒有包含空值。我收到了一個異常,所選項目無效,因爲它不在列表中。

相關問題