我正在尝试编写一个Silverlight应用程序(使用Silverlight 5.1,.Net 4.5),我希望有3个数据网格。
第一个数据网格显示症状列表,第二个数据网格应列出datagrid1中所选症状的子症状,第三个数据网格应显示一种能够治愈子症状的“盐”。 datagrids应通过WCF Webservices接收数据。第一个datagrid从表症状中获取所有数据,第二个datagrids仅从webservice获取datagrid1中所选症状的子样本,而“salt”数据网格只接收与Web服务中所选症状和子症状一起使用的盐。
一旦我选择了症状,就会出现这些次级症状,一旦我选择了子症状,就会出现相应的盐。一切都很好。
我遇到的问题:当我选择一个子症状而不是再次选择Datagrid1中的一个症状时,所有数据网格都会消失。
以下是代码:
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.ServiceModel;
using Rebat.SymptomeService;
namespace Rebat
{
public partial class MainPage : UserControl
{
ServiceClient client = new ServiceClient();
public MainPage()
{
InitializeComponent();
ServiceClient client = new ServiceClient();
client.SymptomeListCompleted += new EventHandler<SymptomeListCompletedEventArgs>(client_SymptomeListCompleted);
client.SymptomeListAsync();
}
void client_SymptomeListCompleted(object sender, SymptomeListCompletedEventArgs e)
{
SymptomeGrid.ItemsSource = e.Result;
}
void client_CustomerListCompleted(object sender, Symptome2ListCompletedEventArgs e)
{
Symptome2Grid.ItemsSource = e.Result;
}
void client_SalzListCompleted(object sender, SalzListCompletedEventArgs e)
{
SalzGrid.ItemsSource = e.Result;
}
private void SymptomeGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Symptome sympt = SymptomeGrid.SelectedItem as Symptome;
client.Symptome2ListCompleted += new EventHandler<Symptome2ListCompletedEventArgs>(client_CustomerListCompleted);
client.Symptome2ListAsync(sympt.sId.ToString());
}
private void Symptome2Grid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Symptome2 sympt2 = Symptome2Grid.SelectedItem as Symptome2;
client.SalzListCompleted += new EventHandler<SalzListCompletedEventArgs>(client_SalzListCompleted);
//if i remove the next line, datagrids don't dissapear anymore... so this might be the problem
client.SalzListAsync(sympt2.sy1.ToString(), sympt2.sy2.ToString());
}
}
}
这里是.xaml代码
<UserControl
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:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="Rebat.MainPage"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White" Margin="-13,0,-543,-335" RenderTransformOrigin="0.499,0.573">
<Grid.RowDefinitions>
<RowDefinition Height="0*"/>
<RowDefinition Height="516*"/>
<RowDefinition Height="119*"/>
</Grid.RowDefinitions>
<sdk:DataGrid x:Name="SymptomeGrid" Margin="20,10,0,0" HorizontalAlignment="Left" Width="249" Grid.RowSpan="2" SelectionChanged="SymptomeGrid_SelectionChanged" Height="227" VerticalAlignment="Top"/>
<sdk:DataGrid x:Name="Symptome2Grid" HorizontalAlignment="Left" Height="227" Margin="293,10,0,0" VerticalAlignment="Top" Width="244" Grid.RowSpan="2" SelectionChanged="Symptome2Grid_SelectionChanged"/>
<sdk:DataGrid x:Name="SalzGrid" HorizontalAlignment="Left" Height="227" Margin="20,275,0,0" Grid.Row="1" VerticalAlignment="Top" Width="401"/>
</Grid>
</UserControl>
这里是网络服务代码:
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
namespace Rebat.Web
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service
{
[OperationContract]
public List<Symptome> SymptomeList()
{
var custList = new List<Symptome>();
using (OleDbConnection conn = new OleDbConnection(
@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\simon\Documents\Visual Studio 2012\Projects\WpfApplication1\WpfApplication1\rebat.mdb"))
{
const string sql = @"SELECT Feld1, Feld2 FROM Symptome";
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(sql, conn))
{
OleDbDataReader dr = cmd.ExecuteReader(
CommandBehavior.CloseConnection);
if (dr != null)
while (dr.Read())
{
var cust = new Symptome
{
sId = dr.GetInt16(0),
symptom = dr.GetString(1)
};
custList.Add(cust);
}
return custList;
}
}
}
// Add more operations here and mark them with [OperationContract]
[OperationContract]
public List<Symptome2> Symptome2List(string s1)
{
var custList = new List<Symptome2>();
using (OleDbConnection conn = new OleDbConnection(
@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\simon\Documents\Visual Studio 2012\Projects\WpfApplication1\WpfApplication1\rebat.mdb"))
{
string sql = @"SELECT ID, sy1, sy2, symptom2 FROM Symptome2 where sy1="+s1;
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(sql, conn))
{
OleDbDataReader dr = cmd.ExecuteReader(
CommandBehavior.CloseConnection);
if (dr != null)
while (dr.Read())
{
var cust = new Symptome2
{
ID = dr.GetInt32(0),
sy1 = dr.GetInt16(1),
sy2 = dr.GetInt16(2),
symptom2 = dr.GetString(3)
};
custList.Add(cust);
}
return custList;
}
}
}
// Add more operations here and mark them with [OperationContract]
[OperationContract]
public List<Salz> SalzList(string sym1, string sym2)
{
var salzList = new List<Salz>();
using (OleDbConnection conn = new OleDbConnection(
@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\simon\Documents\Visual Studio 2012\Projects\WpfApplication1\WpfApplication1\rebat.mdb"))
{
string sql = @"SELECT sId, salz FROM Salze where sId in (SELECT saId from Rezept where sy1Id= " + sym1 + " and sy2Id=" +sym2 + ")";
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(sql, conn))
{
OleDbDataReader dr = cmd.ExecuteReader(
CommandBehavior.CloseConnection);
if (dr != null)
while (dr.Read())
{
var cust = new Salz
{
SID = dr.GetInt32(0),
Name = dr.GetString(1)
};
salzList.Add(cust);
}
return salzList;
}
}
}
}
}
最后是帮助班
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Rebat.Web
{
public class Symptome
{
public int sId { get; set; }
public string symptom { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Rebat.Web
{
public class Symptome2
{
public int ID { get; set; }
public int sy1 { get; set; }
public int sy2 { get; set; }
public string symptom2 { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Rebat.Web
{
public class Salz
{
public int SID { get; set; }
public string Name { get; set; }
}
}
答案 0 :(得分:1)
这是完整的源代码,试试这个
Code behind.cs:
private void SymptomeGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Symptome2Grid.ItemsSource = null;
SalzGrid.ItemsSource = null;
svcSymptom.Symptome sympt = SymptomeGrid.SelectedItem as svcSymptom.Symptome;
if (sympt != null)
{
svcSymptom.Service1Client SymptomsClient = new svcSymptom.Service1Client();
SymptomsClient.Symptome2ListCompleted += (a, ae) =>
{
if (ae.Result.Count != 0)
{
Symptome2Grid.ItemsSource = ae.Result.ToList();
}
};
SymptomsClient.Symptome2ListAsync(sympt.symptom.ToString());
}
}
private void Symptome2Grid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SalzGrid.ItemsSource = null;
svcSymptom.Symptome2 sympt2 = Symptome2Grid.SelectedItem as svcSymptom.Symptome2;
if (sympt2 != null)
{
svcSymptom.Service1Client SymptomsClient = new svcSymptom.Service1Client();
SymptomsClient.SalzListCompleted += (a, ae) =>
{
if (ae.Result.Count != 0)
{
SalzGrid.ItemsSource = ae.Result.ToList();
}
};
SymptomsClient.SalzListAsync(sympt2.sy1.ToString(), sympt2.sy2.ToString());
}
}