Silverght+WCF 如何绑定数据到 DataGrid
新手,刚学 Silverght + wcf
--Model层
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Runtime.Serialization;namespace SilverlightClient.Web.Model{ [DataContract] public class Class { private int id; public int Id { get { return id; } set { id = value; } } private string className; public string ClassName { get { return className; } set { className = value; } } public Class() { } }}
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data;using System.Configuration;using System.Data.SqlClient;using System.Text;using System.Collections;namespace SilverlightClient.Web.DAL{ public abstract class DBHelper { //Database connection strings public static readonly string ConnectionStringLocalTransaction = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString; //public static readonly string ConnectionStringOrderDistributedTransaction = ConfigurationManager.ConnectionStrings["SQLConnString3"].ConnectionString; //public static readonly string ConnectionStringProfile = ConfigurationManager.ConnectionStrings["SQLProfileConnString"].ConnectionString; // Hashtable to store cached parameters private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable()); /// <summary> /// Execute a SqlCommand (that returns no resultset) against the database specified in the connection string /// using the provided parameters. /// </summary> /// <remarks> /// e.g.: /// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24)); /// </remarks> /// <param name="connectionString">a valid connection string for a SqlConnection</param> /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param> /// <param name="commandText">the stored procedure name or T-SQL command</param> /// <param name="commandParameters">an array of SqlParamters used to execute the command</param> /// <returns>an int representing the number of rows affected by the command</returns> public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); using (SqlConnection conn = new SqlConnection(connectionString)) { PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); int val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return val; } } /// <summary> /// Execute a SqlCommand (that returns no resultset) against an existing database connection /// using the provided parameters. /// </summary> /// <remarks> /// e.g.: /// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24)); /// </remarks> /// <param name="conn">an existing database connection</param> /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param> /// <param name="commandText">the stored procedure name or T-SQL command</param> /// <param name="commandParameters">an array of SqlParamters used to execute the command</param> /// <returns>an int representing the number of rows affected by the command</returns> public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); int val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return val; } }}
using System;using System.Collections.Generic;using System.Linq;using System.Web;using SilverlightClient.Web.Model;using System.Data;using System.Data.SqlClient;using System.Configuration;namespace SilverlightClient.Web.DAL{ public class ClassService { /// <summary> /// 获取所有的用户的信息 /// </summary> /// <returns></returns> public List<Class> GetAllClass() { List<Class> Lc = new List<Class>(); string sql = "select * from dbo.Class"; using (SqlDataReader sdr = DBHelper.ExecuteReader(DBHelper.ConnectionStringLocalTransaction, CommandType.Text, sql, null)) { while (sdr.Read()) { Class cl = new Class(); cl.Id = Convert.ToInt32(sdr["ID"]); cl.ClassName = Convert.ToString(sdr["ClassName"]); Lc.Add(cl); } } return Lc; } }}
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data;using System.Data.SqlClient;using SilverlightClient.Web.DAL;using SilverlightClient.Web.Model;namespace SilverlightClient.Web.BLL{ public class ClassManager { ClassService cs = new ClassService(); public List<Class> GetAllClass() { return cs.GetAllClass(); } }}
using System;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.ServiceModel.Activation;using SilverlightClient.Web.BLL;using SilverlightClient.Web.Model;using System.Data;using System.Data.SqlClient;using System.Configuration;using System.Collections.Generic;namespace SilverlightClient.Web{ [ServiceContract(Namespace = "")] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class DBService { ClassManager cm=new ClassManager(); [OperationContract] public void DoWork() { // 在此处添加操作实现 return; } // 在此处添加更多操作并使用 [OperationContract] 标记它们 [OperationContract] public List<Class> GetAllClass() { return cm.GetAllClass(); } }}
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 SilverlightClient.DBServiceReference;namespace SilverlightClient{ public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); //我如何在这里,绑定 控件 ComboBox } }}
<i:Interaction.Behaviors>
<ei:FluidMoveBehavior AppliesTo="Children" Duration="0:0:0.5">
<ei:FluidMoveBehavior.EaseY>
<PowerEase EasingMode="EaseInOut" Power="4"/>
</ei:FluidMoveBehavior.EaseY>
<ei:FluidMoveBehavior.EaseX>
<PowerEase EasingMode="EaseInOut" Power="4"/>
</ei:FluidMoveBehavior.EaseX>
</ei:FluidMoveBehavior>
</i:Interaction.Behaviors>
</toolkit:WrapPanel>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Border Width="190" CornerRadius="5" HorizontalAlignment="Center" BorderThickness="2">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Img}" Width="40" Height="40" HorizontalAlignment="Center" Margin="3,3" VerticalAlignment="Center"></Image>
<TextBlock Text="{Binding Name}" FontSize="14" FontWeight="Bold" Tag="{Binding Id}" Margin="3,3" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</StackPanel>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<ComboBox x:Name="cb" Width="200" Height="44" Style="{StaticResource ComboBoxStyle}" d:LayoutOverrides="Height"/>
</Grid>
[解决办法]
this.ComboBox.ItemsSource = CitySource();
this.ComboBox.DisplayMemberPath = "name";