关于combobox数据绑定显示问题
在做combobox绑定时,我做了2种方式的绑定,但都显示不出数据..
第一种方式,combobox的下来选项在界面上添加
- XML code
<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>
后台代码:
- C# code
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; } }}
最终显示结果:txtbox上显示出了姓名,combobox只显示空白,没有显示出相应的数据,界面上的绑定使用了selectedValue和SelectedItem
第二种方式,使用combobox的itemsource数据源绑定到对象
- XML code
<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>
后台代码:
- C# code
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; } }}
最终结果跟前面一种方法一样,combobox上始终显示不出数据,何解?
sl4中的combobox绑定只有selectedValue和selectedItem2个属性。都试过了绑定上去后还是无法显示数据。
还有个问题,我在第二种方式中在绑定combobox是设置了默认选项(通过selectedIndex=0),结果性别的默认选项显示出来了城市的默认选项没显示出来,我看了界面上的绑定,绑定城市时我用了selectedValue,绑定性别时用了selectedItem。
sl的combobox感觉真不好用,哪位有有好的解决方案?难道要重写combobox?
[解决办法]
楼主在1楼的结论是对的。不过有些不同意见:
1. 和 DisplayMemberPath 无关;
2. SelectedValuePath 和 SelectedValue 本来就是一一对应的关系,微软就是这样设计的,我觉得业务逻辑上也是这样子啊,我不明白楼主为什么会有 People.City (int) 要去和 City.CityId(int)对应这样的逻辑呢?不管是从关系型数据库,还是从面向对象的角度来考虑,People类型都应该记录一个 CityId 才合理啊;
3. 楼主也可以从微软的源代码中(Toolkit中好像能找到),将判断相等SelectedValue的代码给修改一下,把全部类型都转成string类型再进行比较就可以了,DIY成自己的ComboBox。源代码中应是先找到 SelectedItem ,然后根据 SelectedValuePath 得出 SelectedValue ,这一步很可能用的是 Reflection ;
4. ComboBox.SelectedValueProperty 的Binding 是有一个不好用的地方,那就是在进行[Required]的 Validation 时,在“空值”上的表现和 TextBox.TextProperty 的Binding 不同,ComboBox 不选择任何项时,其“空值”是null,而TextBox在不输入任何值时,其“空值”是string.Empty 。
[解决办法]
binding无法显示值,通常来说是因为数据类型绑定错误造成的,对于对象进行绑定到客户端必须进行实例化,如果单单是一个对象,就会产生错误。
[解决办法]
本人觉得不应该在前台绑定!! 最好是在后台绑定
[解决办法]
这是2个绑定了吧 后面combox的绑定 好像没有根据前面grid的条件来绑定 动态绑定 应该是不可以的吧