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?

3
  • 1
    Not sure what you need with that ComboBox property, it smells really bad. Commented May 17, 2016 at 9: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. Commented May 17, 2016 at 9:55
  • 2
    stackoverflow.com/questions/6145888/… Commented May 17, 2016 at 9:56

4 Answers 4

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;
Sign up to request clarification or add additional context in comments.

1 Comment

comboBox.ItemSource = Actions; is this an addition to my code from above?
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.

4 Comments

the list is still empty.
@TonyB Can you see any binding errors in your output? Does the grid have the correct datacontext?
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.
@TonyB Can you show us what you did to make it work?
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.

1 Comment

keeping enmus: because you can add more easily more flags/enum-values. cool
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.