2017-02-16 96 views
0

我是MVVM和xaml的新手。我为PhoneType列表创建了一个组合框,但是因为我已经设置了从数据库加载的PhoneType的绑定不再显示在视图上(运行时)。我绑定的组合框不显示

数据

string SQLPhoneTypes = "select phone_type_id, description from phone_types"; 

//get the phone types for lookups 

MySqlCommand cmdGetTypes = new MySqlCommand(SQLPhoneTypes, ConnectData.connection); 
MySqlDataReader drDataTypes = cmdGetTypes.ExecuteReader(); 
_contact.PhoneTypes = new List<ContactModel.PhoneType>(); 

while (drDataTypes.Read()) 
{ 
    _contact.PhoneTypes.Add(new ContactModel.PhoneType() 
    { 
     PhoneTypeID = drDataTypes.GetInt16("phone_type_id"), 
     Description = drDataTypes.GetString("description") 
    }); 
} 

型号

public List<PhoneType > PhoneTypes 
{ 
    get { return _phonetypes; } 
    set { _phonetypes = value; OnPropertyChanged("PhoneTypes"); } 
} 

视图模型

public ObservableCollection<PhoneTypesLoaded> PhoneTypes { get; set; } = new ObservableCollection<PhoneTypesLoaded>(); 

public class PhoneTypesLoaded 
{ 
    public int phoneTypeID { get; set; } 
    public string description { get; set; } 
} 

if (PhoneTypes.Count == 0) 
{ 
    ContactData.LoadPhoneTypes (c); 
    for (int iCounter = 0; iCounter < c.PhoneTypes.Count; iCounter++) 
    { 
     PhoneTypes.Add(new PhoneTypesLoaded { phoneTypeID = c.PhoneTypes[iCounter].PhoneTypeID, description = c.PhoneTypes[iCounter].Description }); 
    } 
} 

查看

<GroupBox x:Name="grpPhone" Grid.Column="1" Grid.Row="1" VerticalAlignment="Top" HorizontalAlignment="Left" Height="51" Width="200" BorderBrush="{x:Null}" BorderThickness="0"> 
     <ScrollViewer x:Name="pnScrollPhone" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled"> 
      <StackPanel x:Name="pnPhone" Orientation="Vertical" Grid.ColumnSpan="1" HorizontalAlignment="Left" VerticalAlignment="Top" IsEnabled="True"> 
       <ItemsControl ItemsSource="{Binding Path=Phones}"> 
        <ItemsControl.ItemTemplate> 
         <DataTemplate> 
          <Grid> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="30"/> 
            <ColumnDefinition Width="170" /> 
           </Grid.ColumnDefinitions> 
           <ItemsControl ItemsSource="{Binding Path=PhoneTypes}"> 
            <ItemsControl.ItemTemplate> 
             <DataTemplate> 
              <ComboBox x:Name="cboPhoneType" ItemsSource="{Binding description}" Grid.Column="0"/> 
             </DataTemplate> 
            </ItemsControl.ItemTemplate> 
           </ItemsControl> 
           <TextBox x:Name="txtPhone" Text="{Binding phoneNumber, Mode=TwoWay}" Grid.Column="1"/> 
          </Grid> 
         </DataTemplate> 
        </ItemsControl.ItemTemplate> 
       </ItemsControl> 
      </StackPanel> 
     </ScrollViewer> 
    </GroupBox> 

我有什么想法做错了?在此先感谢

回答

0

这是通过聊天解决 - 答案是在Phone类中包含PhoneTypes列表,然后可以绑定到ComboBox。

此外,并不需要内的ItemsControl,除去

+0

难道只有未显示组合框,或在文本框txtPhone没有显示任? –

+0

我刚刚修改了它,如你所建议的,我没有发现你已经删除了它上面的ItemsControl和数据模板。现在看起来像这样 –

+0

<组合框X:名称= “cboPhoneType” 的ItemsSource = “{Binding Path = PhoneTypes}”DisplayMemberPath =“description”/>