2

I am trying to bind the ComboBox below to the list of Characters in the ObservableCollection, but it wont show anything. Any ideas why?

XAML:

    <TabControl ItemsSource ="{Binding TextEditors}"
     <TabControl.ContentTemplate>
      <DataTemplate>
       <ListBox> ItemsSource="{Binding TextLines}"
        <ListBox.ItemTemplate>
         <DataTemplate>
          <Grid>


               <ComboBox 
                   ItemsSource="{Binding DataContext.InvCharacter, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}" 
                   DisplayMemberPath="name" 
                   SelectedValuePath="cid" 
                   SelectedValue="{Binding cid}">
               </ComboBox>


          </Grid>
         </DataTemplate>
        </ListBox.ItemTemplate>
       </ListBox>
      </DataTemplate>
     </TabControl.ContentTemplate>
    </TabControl>

this is the class i am referring to:

     class TextEditorVM: IViewModel {
         public ObservableCollection<TextLineVM> TextLines { get { return textLines; } set { textLines = value;} }
         public ObservableCollection<T_Character> InvCharacters { get { return invCharacters; } set { invCharacters = value; } }


         public TextEditorVM(T_Dialogue dialogue)
         {

             DialogueManager.Instance.Register(this);
             this.TextLines = new ObservableCollection<TextLineVM>();
             this.InvCharacters = new ObservableCollection<T_Character>();
         }
    }

and the MainVM:

     class MainVM : IViewModel
     {
           public ObservableCollection<TextEditorVM> TextEditors { get { return textEditors; } set { textEditors = value; OnPropertyChanged("TextEditors"); } 
     }

my T_Character Class looks like this now :

    public class T_Character
    {

       public String cid { get; set; }
       public String name { get; set; }

       public T_Character(String cid, String name)
       {
          this.cid = cid;
          this.name = name;
       }
    }

2 Answers 2

2

The DataContext of the TabControl is of type MainVM. The RelativeSource of the ComboBox binding should not be the TabControl but rather the ListBox.

Sign up to request clarification or add additional context in comments.

Comments

0

Your InvCharacters property is on your TextEditorVM object which is in your ObservableCollection, however your binding is referencing TabControl.DataContext, which is MainVM, and does not contain that property.

Switch your RelativeSource binding to reference TabItem (it gets created automatically when you bind TabControl.ItemsSource) or ListBox to reference your TextEditorVM object

<ComboBox ItemsSource="{Binding DataContext.InvCharacters, 
              RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}">
</ComboBox>

1 Comment

Thank you so far, i have made some changes in the XAML due to your example. Also i have posted the T_Character Class above. I think i need to change the SelectedValue part but i dont know how. Do you have any idea?

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.