2

以下の ComboBox を ObservableCollection の文字のリストにバインドしようとしていますが、何も表示されません。理由はありますか?

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>

これは私が言及しているクラスです:

     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>();
         }
    }

および MainVM:

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

私の T_Character クラスは次のようになります。

    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;
       }
    }
4

2 に答える 2

2

DataContextTabControlタイプMainVMです。バインディングのRelativeSourceはではなく、です。ComboBoxTabControlListBox

于 2013-01-16T14:01:27.593 に答える
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>
于 2013-01-16T14:45:38.027 に答える