开发者

WPF - How to bind generic method in XAML?

I have a class with a static method that returns a Dictionary. The signature of the method is as follows: public static Dictionary<int, string> CodeLookup<T>() where开发者_如何学JAVA T : EntityCodeBase

At the moment, I'm using this method to bind to my comboboxes in the code behind, like so: this.cboState.ItemsSource = CodeCache.CodeLookup<StateCode>();

Would someone please be able to point me in the correct direction for doing this in XAML so that I can remove this kind of stuff from my codebehind?

Thanks,

Sonny


Not directly binding to a particular method. You should create a property and bound that to it.

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


looks like you cannot do it for generic methods

more info


Here is a property in my viewModel:

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

And here is my 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>

Just set the DataContext to the ViewModel and you get everything you need. Check out MVVM pattern for more info.

There are ways to bind to a static method but if all you are doing is basic databinding then it's a bit of overkill. Take a look at Actions if you are interested.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜