在Silverlight中怎样将返回的对象绑定到模版列上

zhentian 2010-04-06 04:10:35
我从数据库中返回一个结果集(学历信息),类型为ObservableCollection<DegreeData>,怎么将它绑定到模版列ComboBox上
...全文
86 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
NoQinQin 2010-04-15
  • 打赏
  • 举报
回复
怎样动态绑定xml数据?
websco 2010-04-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 rt112000 的回复:]
如果数据中只记录部门id的话,可以用combobox绑定一个所有部门名字的List<string>, selecteditem的绑定中加个converter做部门ID和部门名字转化。。
代码中假定只有2个部门:
ID Name
0 “A”
1 “B”

XML code

<UserControl x:Class="DataBindingSimple.MainPage"
……
[/Quote]

搞一个SelectedItem的Converter(硬编码的味道)还不如派生ComboBox,加上 SelectedValue 依赖项属性,一了百了。
rt112000 2010-04-06
  • 打赏
  • 举报
回复
如果数据中只记录部门id的话,可以用combobox绑定一个所有部门名字的List<string>, selecteditem的绑定中加个converter做部门ID和部门名字转化。。
代码中假定只有2个部门:
ID Name
0 “A”
1 “B”

<UserControl x:Class="DataBindingSimple.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"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
mc:Ignorable="d"
xmlns:local="clr-namespace:DataBindingSimple"
d:DesignHeight="300" d:DesignWidth="400" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data">
<UserControl.Resources>
<local:DepartmentList x:Key="departmentList" />
<local:DepartmentIDToDepartmentConverter x:Key="converter" />
</UserControl.Resources>
<StackPanel x:Name="LayoutRoot" Background="White">
<data:DataGrid x:Name="dataGrid1" AutoGenerateColumns="False">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="Employ ID" Binding="{Binding EmpID}" />
<data:DataGridTextColumn Header="Name" Binding="{Binding EmpName}" />
<data:DataGridTemplateColumn Header="Department">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding EmpDepID, Converter={StaticResource converter}}" />
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
<data:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{StaticResource departmentList}"
SelectedItem="{Binding EmpDepID, Mode=TwoWay, Converter={StaticResource converter}}" />
</DataTemplate>
</data:DataGridTemplateColumn.CellEditingTemplate>
</data:DataGridTemplateColumn>
</data:DataGrid.Columns>
</data:DataGrid>
</StackPanel>
</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;
using System.Collections.ObjectModel;
using System.Windows.Data;

namespace DataBindingSimple
{
public partial class MainPage : UserControl
{

List<Employee> employeeList;

public MainPage()
{
InitializeComponent();

employeeList = new List<Employee>()
{
new Employee{EmpID=0, EmpName="000", EmpDepID=0},
new Employee{EmpID=1, EmpName="111", EmpDepID=1},
new Employee{EmpID=2, EmpName="222", EmpDepID=0},
new Employee{EmpID=3, EmpName="333", EmpDepID=1},
new Employee{EmpID=4, EmpName="444", EmpDepID=1},
new Employee{EmpID=5, EmpName="555", EmpDepID=0}
};

dataGrid1.ItemsSource = employeeList;
}
}

public class Employee
{
public int EmpID { get; set; }
public string EmpName { get; set; }
public int EmpDepID { get; set; }
}

public class Department
{
public int DepID { get; set; }
public string DepName { get; set; }
}

//used as static reource on mainpage, bound to ComboBox in DataTemplate
public class DepartmentList : List<string>
{
public DepartmentList()
{
Add("A");
Add("B");
}
}

//This class maintains department ID to Name mapping.
public class DepartmentIDToDepartmentConverter : IValueConverter
{
//department ID => Name
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
switch ((int)value)
{
case 0: return "A";
case 1: return "B";
default: return "NA";
}

}
//Department Name => ID
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
switch ((string)value)
{
case "A": return 0;
case "B": return 1;
default: return -1;
}
}
}

}

rt112000 2010-04-06
  • 打赏
  • 举报
回复
你好像发了3个这方面的帖子。。还是接着你第一个贴的例子,先不要谈combobox,datagrid中部门列binding的是一个int 部门ID,还是部门object? 如果是部门object的话,我见过的办法就是修改员工class, 在员工class里加个所有部门的List, 名字为AllDeparments。 这样你在datagrid中的就可以写<combobox itemssource="{binding AllDepartments}" selecteditem="{binding Department, mode=twoway"}.
sky-defender 2010-04-06
  • 打赏
  • 举报
回复

void client_GetDataBaseCompleted(object sender, GetDataBaseCompletedEventArgs e)
{
for (int i = 0; i < e.Result.Count; i++)
{
cmbDataBaseList.Items.Add(e.Result[i].ToString());
}

}
sky-defender 2010-04-06
  • 打赏
  • 举报
回复
Combobox.Items.add(e.result[i].Name)

8,744

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧