2010-11-24 33 views
0

我有一個類,返回一個字典的靜態方法。該方法的簽名如下: public static Dictionary<int, string> CodeLookup<T>() where T : EntityCodeBaseWPF - 如何在XAML中綁定泛型方法?

目前,我使用這個方法綁定到我的代碼組合框的背後,像這樣: this.cboState.ItemsSource = CodeCache.CodeLookup<StateCode>();

會有人請能夠指向我在XAML中這樣做的正確方向,以便我可以從我的代碼隱藏中刪除這種東西?

謝謝,
桑尼

+0

這曾經是我最想要的功能之一。現在我使用ViewModel將該函數作爲只讀屬性公開。 – 2010-11-24 00:51:27

+0

@Jon,有時這樣做很方便,但我認爲它使View模板過於依賴於後端實現。 – xandy 2010-11-24 02:17:20

回答

4

不直接結合到一個特定的方法。你應該創建一個屬性並將其綁定到它。

public Dictionary<int, string> Code { 
    get { return CodeCache.CodeLookup<StateCode>(); } 
} 
0

看起來就像你不能對泛型方法

more info

0

這裏做的是我的ViewModel屬性:

public ObservableCollection<Contact> AllContacts 
    { 
     get { return _applicationViewModel.CurrentContacts; } 
    } 

這裏是我的XAML:

<ListView Margin="5" ItemsSource="{Binding Path=AllContacts}"> 
     <ListView.View> 
      <GridView> 
       <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding Path=LastName}" /> 
       <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding Path=FirstName}" /> 
       <GridViewColumn Header="Work Phone" DisplayMemberBinding="{Binding Path=OfficePhone, Converter={StaticResource phoneConverter}}" /> 
       <GridViewColumn Header="Cell Phone" DisplayMemberBinding="{Binding Path=CellPhone, Converter={StaticResource phoneConverter}}" /> 
       <GridViewColumn Header="Email Address" DisplayMemberBinding="{Binding Path=PrimaryEmail}" /> 
      </GridView> 
     </ListView.View> 
    </ListView> 

只需將DataContext設置爲ViewModel,即可獲得所需的一切。查看MVVM模式瞭解更多信息。

有辦法綁定到靜態方法,但如果你所做的只是基本的數據綁定,那麼它有點矯枉過正。如果您有興趣,請查看Actions。