1
votes

all,

I am really stuck on a very basic concept - I just cannot find an answer that I really understand.

I have a view - viewContacts. Its datacontext is a viewmodel - viewmodelContacts.

On the view I have a combobox for 'titles' (you know ... Mr, Mrs, Miss, etc). I want to bind the itemssource so it shows a list of options, and bind the SelectedValue so that it stores the key in the viewmodelContacts TitleId property.

The 'source' list if titles (that I want to bind ItemsSource to) is actually in a separate static class - note NOT the viewmodel!

internal static class Titles
{
    static IList<Title> _colTitles = null;
    static Titles()
    {
        _colTitles = new List<Title>();
        _colTitles.Add(new Title() { TitleId = 1, Description = "Mr." });
        _colTitles.Add(new Title() { TitleId = 2, Description = "Mrs." });
        _colTitles.Add(new Title() { TitleId = 3, Description = "Miss." });
        _colTitles.Add(new Title() { TitleId = 4, Description = "Ms." });
    }

    internal static IList<Title> GetTitles()
    {
        return _colTitles;
    }
}

(later on, of course, this stuff will be fetched from a database).

This simple task exceeds my talent as a WPF developer.

How do I set the ItemsSource to this static class? How do I further set the DisplayMemberPath to the Description property of the underlying Title object?

What do I set the SelectedValue and SelectedValuePath to?

Really hoping someone can help me out with what appears to be a simple problem that I just cannot work out.

Thanks, Gray

3
Don't have time for a full-blown answer, but why not just create a property in your ViewModel that returns the data from your "Model?" In this case, your model is the static class - later, it will be whatever you use to get the data from the database. That way, your View binding will not change when you change your Model (which is one of the main points of MVVM - separation of concerns).Wonko the Sane

3 Answers

2
votes

In your viewModel create a property like so:

public IList<Title> VmTitles
{
    get
    {
        return Titles.GetTitles();
    }
}

private Title _selectedTitle;
public Title SelectedTitle
{
    get
    {
        return _selectedTitle;
    }
    set
    {
        if (value != _selectedTitle)
        {
            _selectedTitle = value;
            OnPropertyChanged("SelectedTitle");
        }
    }
}

Then in your .xaml you do this:

<ComboBox x:Name="cboTitle" ItemsSource="{Binding Path=VmTitles}" DisplayMemberPath="Description" SelectedItem="{Binding Path=SelectedTitle}" />
1
votes
<ComboBox x:Name="cboTitle" SelectedValuePath="TitleId" DisplayValuePath="Description" SelectedValue="{Binding PropertyToBeUpdatedHere}"/>

then set the combo box's item source in your code..

cboTitle.ItemsSource = Titles.GetTitles();
0
votes

simply use a ObjectDataProvider.

<UserControl.Resources>
    <ObjectDataProvider x:Key="cboSource" ObjectType="local:Titles" MethodName="GetTitles"/>
</UserControl.Resources>

    <ComboBox ItemsSource="{Binding Source={StaticResource cboSource}}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Description}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

ps: you have to change your internal static to public static.