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>
我有什麼想法做錯了?在此先感謝
難道只有未顯示組合框,或在文本框txtPhone沒有顯示任? –
我剛剛修改了它,如你所建議的,我沒有發現你已經刪除了它上面的ItemsControl和數據模板。現在看起來像這樣 –