关于combobox数据绑定显示问题
在做combobox绑定时,我做了2种方式的绑定,但都显示不出数据..
第一种方式,combobox的下来选项在界面上添加
<UserControl x:Class="SilverlightApplication2.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="317" d:DesignWidth="431" Loaded="UserControl_Loaded"> <Grid x:Name="LayoutRoot" Background="White"> <Grid.ColumnDefinitions> <ColumnDefinition Width="114*" /> <ColumnDefinition Width="144*" /> <ColumnDefinition Width="142*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="48*" /> <RowDefinition Height="37*" /> <RowDefinition Height="39*" /> <RowDefinition Height="43*" /> <RowDefinition Height="133*" /> </Grid.RowDefinitions> <ComboBox Height="23" SelectedItem="{Binding Path=Sex,Mode=TwoWay}" Name="comboBox1" Grid.Row="2" Grid.Column="1"> <ComboBoxItem>先生</ComboBoxItem> <ComboBoxItem>女士</ComboBoxItem> </ComboBox> <ComboBox Height="23" SelectedValue="{Binding Path=City,Mode=TwoWay}" Name="comboBox2" Grid.Row="3" Grid.Column="1"> <ComboBoxItem>北京</ComboBoxItem> <ComboBoxItem>上海</ComboBoxItem> <ComboBoxItem>深圳</ComboBoxItem> </ComboBox> <TextBox Height="23" Text="{Binding Path=Name,Mode=TwoWay}" Name="textBox1" Grid.Column="1" Grid.Row="1" /> </Grid></UserControl>
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;namespace SilverlightApplication2{ public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } private void UserControl_Loaded(object sender, RoutedEventArgs e) { People p= new People {Name="lkj",Sex="先生",City="北京" }; LayoutRoot.DataContext = p; } } public class People { public string Name { get; set; } public string Sex { get; set; } public string City { get; set; } }}
<UserControl x:Class="SilverlightApplication3.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400" Loaded="UserControl_Loaded"> <Grid x:Name="LayoutRoot" Background="White"> <Grid.ColumnDefinitions> <ColumnDefinition Width="114*" /> <ColumnDefinition Width="144*" /> <ColumnDefinition Width="142*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="48*" /> <RowDefinition Height="37*" /> <RowDefinition Height="39*" /> <RowDefinition Height="43*" /> <RowDefinition Height="133*" /> </Grid.RowDefinitions> <ComboBox Height="23" DisplayMemberPath="SexName" SelectedValuePath="SexID" SelectedItem="{Binding Path=Sex,Mode=TwoWay}" Name="cboSex" Grid.Row="2" Grid.Column="1"> </ComboBox> <ComboBox Height="23" DisplayMemberPath="CityName" SelectedValuePath="CityID" SelectedValue="{Binding Path=City,Mode=TwoWay}" Name="cboCity" Grid.Row="3" Grid.Column="1"> </ComboBox> <TextBox Height="23" Text="{Binding Path=Name,Mode=TwoWay}" Name="txtName" Grid.Column="1" Grid.Row="1" /> </Grid></UserControl>
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;namespace SilverlightApplication3{ public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } private void UserControl_Loaded(object sender, RoutedEventArgs e) { //给ComboBox添加数据源 //性别 List<Sex> sexs = new List<Sex>(); sexs.Add(new Sex { SexID=1,SexName="男"}); sexs.Add(new Sex { SexID =0, SexName = "女" }); cboSex.ItemsSource = sexs; cboSex.SelectedIndex = 0;//初始化默认选项 //城市 List<City> citys = new List<City>(); citys.Add(new City { CityID = 0, CityName = "北京" }); citys.Add(new City { CityID = 1, CityName = "上海" }); citys.Add(new City { CityID = 2, CityName = "深圳" }); cboCity.ItemsSource = citys; cboCity.SelectedIndex = 0;//初始化默认选项 People p = new People { Name = "lkj", Sex = "先生", City = "北京" }; LayoutRoot.DataContext = p; } } public class Sex { public string SexName { get; set; } public int SexID { get; set; } } public class City { public string CityName { get; set; } public int CityID { get; set; } } public class People { public string Name { get; set; } public string Sex { get; set; } public string City { get; set; } }}