SL中必须点两次按钮才能将实体绑定到数据源
接触SL有20多天了,由于以前都是用DataSet做程序,一下子都换成实体,不是太习惯,也有很多不明白的地方。
这个问题困扰我好多天了,以前向GridControl绑定数据源的时候,点击查询按钮没有反应,我以为是LINQ查询语句写的有问题,谁知再点一次数据就都显示出来了。。但是原因不知道是什么,无奈我在PageLoad里就触发一次点击按钮事件,再点的话一下就能显示数据了。
下面是我写的绑定GridView的代码。
SummaryDomainContext context = new SummaryDomainContext(); //点击按钮事件 private void btnSearch_Click(object sender, RoutedEventArgs e) { if (cmbUsername.SelectedItem != null) { string uname = (cmbUsername.SelectedItem as TB_SUMMARYUSER).USERNAME.ToString(); Search(uname); } else { Helper.ShowMsg("操作失败", "用户名不正确,请选择用户名"); } } private void Search(string uname) { context.Load(context.GetTB_SUMMARYCONTENTQuery()); var result = from r in context.TB_SUMMARYCONTENTs where (Convert.ToDateTime(r.RECORDDATE).CompareTo(Convert.ToDateTime(dateBegin.Text)) >= 0) && (Convert.ToDateTime(r.RECORDDATE).CompareTo(Convert.ToDateTime(dateEnd.Text)) <= 0) select r; if (uname != "全部") { result = result.Where(r => r.USERNAME == uname); } if (!string.IsNullOrEmpty(txtContentKeyword.Text)) { result = result.Where(r => r.RECORDCONTENT.Contains(txtContentKeyword.Text)); } //绑定数据源 this.gridViewSummary.DataContext = result; this.gridViewSummary.SetBinding(GridControl.ItemsSourceProperty, new Binding()); } private void Page_Loaded(object sender, RoutedEventArgs e) { Search(username);//防止按钮点击两次才有效果,在页面加载就查询一次 }
DomainServiceTest context = new DomainServiceTest(); string username = SessionManager.GetSession("username"); private void Page_Loaded(object sender, RoutedEventArgs e) { GenerateMenu(); //页面加载完毕调用,但是无效 } private void button1_Click(object sender, RoutedEventArgs e) { GenerateMenu(); //用按钮再点击一下就出来效果了 } private void GenerateMenu() { List<V_LISTRIGHT> list = context.V_LISTRIGHTs.Where(r => r.PARENTNADOID == "0").ToList(); foreach (V_LISTRIGHT tmp in list) { NavBarGroup newGroup = new NavBarGroup(); newGroup.Header = tmp.DISPLAYNAME; navBarList.Groups.Add(newGroup); foreach (V_LISTRIGHT item in context.V_LISTRIGHTs) { if (item.PARENTNADOID == tmp.NODEID) { NavBarItem newItem = new NavBarItem(); newItem.Content = item.DISPLAYNAME; newGroup.Items.Add(newItem); newItem.Click +=new EventHandler(newItem_Click); } } } }
2、void client_GetDataCompleted(object sender, GetDataCompletedEventArgs e)
定义回调函数的内容
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
//创建一个代理类的实例
SLWCFRIAClient client = new SLWCFRIAClient();
//调用GetData方法并加载事件
client.GetDataAsync();
client.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(client_GetDataCompleted);
}
public List<UserInfo> userList = new List<UserInfo>();
void client_GetDataCompleted(object sender, GetDataCompletedEventArgs e)
{
using (XmlReader xReader = XmlReader.Create(new StringReader(e.Result)))
{
//XmlReader读取XML数据
while (xReader.ReadToFollowing("Table"))
{
xReader.ReadToDescendant("ID");
string id = xReader.ReadElementContentAsString();
xReader.ReadToNextSibling("UserName");
string username = xReader.ReadElementContentAsString();
xReader.ReadToNextSibling("UserAddr");
string useraddr = xReader.ReadElementContentAsString();
xReader.ReadToNextSibling("UserTel");
string usertel = xReader.ReadElementContentAsString();
//实例化类并添加进实体类List<>
UserInfo uinfo = new UserInfo(){ID=id, UserName=username,UserAddr=useraddr,UserTel=usertel};
userList.Add(uinfo);
}
this.grShow.ItemsSource = userList;
}
}
}