0

i'm trying to bind a ComboBox from code behind to XAML. This is my XAML part:

<GridViewColumn Header="Action Type">
   <GridViewColumn.CellTemplate>
     <DataTemplate>
        <ComboBox x:Name="comboBox" ItemsSource="{Binding ActionType}"/>
     </DataTemplate>
   </GridViewColumn.CellTemplate>
 </GridViewColumn>

Before the itemsources are bound (done in code), i prepare the datasets and create the controls in code (with some specifiy properties). now i want to bind the comboBox XAML to my C# code comboBox element, for example:

  public ComboBox ActionType { get; set; }

how can i perform such a task? generally, is it possible to attach controls from the code into the XAML part?

Tony B
  • 344
  • 5
  • 19
  • 1
    Not sure what you need with that ComboBox property, it smells really bad. – Natxo May 17 '16 at 09:51
  • I want to add an enum to that combobox and bind it after the items are populated. -> currently the combo box is empty with the binding. – Tony B May 17 '16 at 09:55
  • 2
    http://stackoverflow.com/questions/6145888/how-to-bind-an-enum-to-a-combobox-control-in-wpf – Pikoh May 17 '16 at 09:56

4 Answers4

1

You just need to set the ItemSource property of the combo box.

List<ActionTypes> Actions = new List<ActionTypes>();

Actions.add(); etc.....

comboBox.ItemSource = Actions;
Ben Steele
  • 414
  • 2
  • 10
1

Define your property as it should:

public Ilist<string> ActionType
{
   get { return _actionType; }
   set
   {
      _actionType= value;
      OnPropertyChanged("ActionType");
   }
}

When you set its value it will be notified to the ComboBox without dirty codebehind.

Natxo
  • 2,917
  • 1
  • 24
  • 36
  • the list is still empty. – Tony B May 17 '16 at 11:11
  • @TonyB Can you see any binding errors in your output? Does the grid have the correct datacontext? – Natxo May 17 '16 at 11:16
  • I used an observable collection, added items there and set to the combobox the itemsource. this was obviously not working. Somehow i didn't get your example to work, i change a bit and it worked. – Tony B May 17 '16 at 11:44
  • @TonyB Can you show us what you did to make it work? – Natxo May 26 '16 at 08:19
0

In order to get this to work i've made this one, i've changed:

C# // getter setter public List ActionTypeList { get; set; }

// init
ActionTypeList = Enum.GetNames(typeof(Data.Structures.ActionType)).ToList();

XAML // itemsource to list

it's a not the best way to get an enum type to a combobox working. after selection and action i switch case on the string. somehow working with enums and combobox in the wpf field feels dirty....i rethink it and may remove the enum again.

Tony B
  • 344
  • 5
  • 19
0

Here's the solution I worked out.

The Enum is called "ActionToggle". Then I did following:

In MainWindow.xaml.cs I defined:

public List<string> ActionToggleList { get; set; } 

Then I bound this list on the ComboBox like that:

ComboBox x:Name="comboBox" ItemsSource="{Binding ActionToggleList}" SelectedItem="{Binding ActionToggleSelected}" SelectionChanged="OnActionToggleComboBoxChanged"/>

When MainWindow is initialized, i've added:

ActionToggleList = new List<string>();
ActionToggleList = Enum.GetNames(typeof(ActionToggle)).ToList();

Another important step is to make some evalulations in the function OnActionToggleComboBoxChanged. Just check "ActionToggleSelected to all cases of ActionToggle possibilities.

private void OnActionToggleComboBoxChanged(object sender, SelectionChangedEventArgs e)
{
  ComboBox c = sender as ComboBox;
   if(c.Items.Count > 0)
   {
     // make it switch-case on string or if-clauses:     
     if (ActionToggleSelected.Equals(ActionToggle.none.ToString()))
      {
         // do some stuff
      }
      if (ActionToggleSelected.Equals(ActionToggle.start.ToString()))
      {
         // do some stuff
      }
   } 
}

maybe not the best 100% WPF-stlyed solution, but a straight forward one i guess. This is only good if the enum is a small one. maybe another, more dynamic solution could be used if the enum is a big one.

Tony B
  • 344
  • 5
  • 19