XAML ComboBox Binds to Property

If I have a property in C #;

    public CollectionView Months
    {
        get 
        {
            CollectionView retList = new Enumerations.Months().ToCollectionView<Enumerations.Months>();
            return retList;
        }
    }

And I have a ComboBox;

<ComboBox x:Name="ddlMonth" Grid.Row="3" Grid.Column="1" 
ItemsSource="{Binding Source={StaticResource Months}}"/>

How can I link my ComboBox to my property?

I must add that I am a complete haml newbie.

+3
source share
1 answer

At first, this is not a method - this is a property.

All you have to do is the following:

<ComboBox x:Name="ddlMonth" Grid.Row="3" Grid.Column="1" ItemsSource="{Binding Months}"/>

The trick is to make sure that any class that has the property Monthsis set DataContextfor either ComboBox or one of its parents (including the page).

So, if a property Monthsappears on the same page that hosts the ComboBox, you can add this line of code to the constructor or event Loadedon the page:

this.DataContext = this;

Months ViewModel ( , !), ViewModel DataContext ( , () ComboBox).

+2

Source: https://habr.com/ru/post/1743762/


All Articles