我绑定的组合框没有显示

时间:2017-02-16 11:45:44

标签: c# wpf mvvm combobox

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

数据

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>

任何想法我做错了什么?提前致谢

1 个答案:

答案 0 :(得分:0)

这是通过聊天解决的 - 答案是包含一个List&lt;&gt; Phone类中的PhoneTypes,然后可以绑定到ComboBox。

此外,不需要内部ItemsControl,并已删除