0

My Enum is not binding to my Contact Manager.

I have an Enum Class

    [Serializable]
    public enum Group
    {
        Friend,
        Family,
        Coworker
    }
}

Then I have my MainWindow which initializes my Contact Manager

public partial class MainWindow : Window
    {
        //public List<Contact> ContactList = new List<Contact>();
        public ObservableCollection<Contact> ContactList = new ObservableCollection<Contact>();



        string fileName = null;


        Contact furqan = new Contact("Herp Derp");
        Contact rizwan = new Contact("Merp Meep");

        public MainWindow()
        {
            InitializeComponent();
            myItemsControl.ItemsSource = ContactList;
            //myComboBox.ItemsSource = Enum.GetValues(typeof(Group));
            //myComboBox.ItemsSource = Enum.GetValues(typeof(Group)).Cast<Group>();


            furqan.HomePhone = "801238421";
            ContactList.Add(furqan);
            ContactList.Add(rizwan);


            this.DataContext = this;
        }

My XAML looks like this

    <Label Grid.Row="7" Content="Home Email:"/>
    <TextBox Grid.Row="7" Text="{Binding ElementName=myItemsControl, Path=SelectedItem.PersonalEmail, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

    <Label Grid.Row="8" Content="Work Email:"/>
    <TextBox Grid.Row="8" Text="{Binding ElementName=myItemsControl, Path=SelectedItem.WorkEmail, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>


    <Label Grid.Row="10" Content="Group:"/>
    <ComboBox Grid.Row="10" Grid.Column="1" Text="{Binding ElementName=myItemsControl, Path=SelectedItem.ContactGroup, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

The ComboBox should display "CoWorker, Family, Friend" inside of it, but it doesn't, and it should bind with my Contact Class.

http://pastebin.com/BtY7SSjR

<ListBox x:Name="myItemsControl" Grid.Column="0" Grid.Row="1" Background="LightBlue">
            <ItemsControl.ItemTemplate>
                <DataTemplate x:Name="myDataTemplate">
                    <StackPanel>
                        <TextBlock Height="50" x:Name="contactName">
                            <Run FontSize="14" Text="{Binding Path=FirstName}"/>
                            <Run FontSize="14" Text="{Binding Path=LastName}"/>
                        </TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ListBox>

How do I correctly bind this so it works with my Contact?

EDIT Everything works except for my Enums

http://puu.sh/b3JDK.jpg

All textboxes show what they are supposed to. Only the ComboBox doesn't work because it's not binded to all of the values that the Enum can be. I need to make an IEnumerable, maybe a ObservableCollection and add all of the values of the Group to it, and then bind to it. That works, however I have no idea how to bind my contactList(the observablecollection that's holding all of my contacts) to hold the Group collection. Any ideas?

Thanks!

Technocrat
  • 334
  • 1
  • 4
  • 15
  • Potentially relevant: http://stackoverflow.com/questions/6145888/how-to-bind-an-enum-to-a-combobox-control-in-wpf – Chris Aug 22 '14 at 22:33
  • I've seen that already, and it didn't quite help. It wasn't for my exact purpose. – Technocrat Aug 22 '14 at 22:35
  • are you sure the binding works ? do the textboxs show what they are suppose to ? – eran otzap Aug 22 '14 at 22:40
  • All textboxes show what they are supposed to. Only the ComboBox doesn't work because it's not binded to all of the values that the Enum can be. I need to make an IEnumerable, maybe a ObservableCollection and add all of the values of the Group to it, and then bind to it. That works, however I have no idea how to bind my contactList(the observablecollection that's holding all of my contacts) to hold the Group collection. Any ideas? – Technocrat Aug 22 '14 at 22:45
  • duplicate question: https://stackoverflow.com/questions/6145888/how-to-bind-an-enum-to-a-combobox-control-in-wpf – Nick Sep 03 '15 at 13:30

1 Answers1

2

I fixed it by doing the following:

<ComboBox ItemsSource="{Binding ElementName=myItemsControl, Path=SelectedItem.ContactGroupValues, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                      SelectedItem="{Binding ElementName=myItemsControl, Path=SelectedItem.ContactGroup, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Row="10" Grid.Column="1"/>

and then in the Contact class:

private Group _Group;

        public Group ContactGroup
        {
            get
            {
                return _Group;
            }
            set
            {
                _Group = value;
            }
        }

        public IEnumerable<Group> ContactGroupValues
        {
            get
            {
                return Enum.GetValues(typeof(Group)).Cast<Group>();
            }
        }
Technocrat
  • 334
  • 1
  • 4
  • 15