快捷搜索:

Silverlight+WCF实现一个弹出选择框---Part 2

第六步: 在Silverlight的项目中添加WCF的Service接口:

首先必要转换WCF办事,编译这个项目,然后在PopFormDemo的Reference部分邮件选择’Add Service Reference’,如下图所示。

然后会呈现如下的提示框,在Address栏位输入上面的那个测试url,然后点击go,也便是调用了那个WCF Service,同时会天生一个app.config文件来保存地址,绑定信息等。如下图所示:

Pop search form是放在一个Border控件中,以是可以应用Visibility属性来显示或暗藏pop search form,如下图所示:

初始状态下,pop search form控件的Visibility=”Collapsed”, 如下图:

我在App.xaml文件中定义了Resources。由于每个元素都能够是以造访resources.你也可以再page层定义resources.

第八步:在PopForm.xaml.cs文件中有下面5个主要属性:

public string DataProvider { get; set; }

public string TitleName { get; set; }

public string PopId { get; set; }

public string PopName { get; set; }

public string OpacityValue { get; set; }

DataProvider: It tells which data you would like to show on pop search form

TitleName: Title name on the top of pop search form

PopId: Label ID text on the pop search form

PopName: Label NAME text on the pop search form

OpacityValue: Opacity level for the pop search form

如前面所说,这里会有两种要领来显示pop search form:

1.

a.点击OK按钮:

private void btnPopEnter_Click(object sender, RoutedEventArgs e)

{

if (popBoder.Visibility == Visibility.Visible)

{

PopFormClass pf = dgPop.SelectedItem as PopFormClass;

txtId.Text = pf.ID;

txtName.Text = pf.NAME;

popBoder.Visibility = Visibility.Collapsed;

}

}

b.双击Datagrid的某条数据应用MouseClickManager.Assign的doubleclick事故:

public PopForm()

{

InitializeComponent();

this.Loaded += new RoutedEventHandler(PopForm_Loaded);

_gridClickManager = new MouseClickManager(200);

_gridClickManager.DoubleClick += new MouseButtonEventHandler(ResultSelected);

}

private void ResultSelected(object sender, MouseButtonEventArgs e)

{

if (popBoder.Visibility == Visibility.Visible)

{

PopFormClass pf = dgPop.SelectedItem as PopFormClass;

txtId.Text = pf.ID;

txtName.Text = pf.NAME;

popBoder.Visibility = Visibility.Collapsed;

}

}

2.从Textbox来获得:

private void txtId_MouseLeave(object sender, MouseEventArgs e)

{

if (!string.IsNullOrEmpty(txtId.Text))

{

var eData = from eFilter in popData

where eFilter.ID == txtId.Text

select eFilter;if (eData.Count() > 0)

{

txtName.Text = eData.First().NAME;

}

else

{

txtName.Text = string.Empty;

}

}

}

着末这段代码是用来把WCF的Service结果转换为datagrid的数据源:

private void dgPop_Loaded(object sender, RoutedEventArgs e) {

lblPopTitle.Text = string.IsNullOrEmpty(TitleName) ? "" : TitleName;

lblPopId.Text = string.IsNullOrEmpty(PopId) ? "ID: " : PopId;

lblPopName.Text = string.IsNullOrEmpty(PopName) ? "NAME: " : PopName;

if (!string.IsNullOrEmpty(DataProvider)) { switch (DataProvider) { case

"GetSqlCustomersData": daoWcf = new PopDataServiceClient();

daoWcf.GetSqlCustomersDataCompleted += new

EventHandler(daoWcf_GetSqlCustomersDataCompleted);

daoWcf.GetSqlCustomersDataAsync(); break; case "GetSqlProductsData":

daoWcf = new PopDataServiceClient(); daoWcf.GetSqlProductsDataCompleted

+= new

EventHandler(daoWcf_GetSqlProductsDataCompleted);

daoWcf.GetSqlProductsDataAsync(); break; case "GetSqlEmployeesData":

daoWcf = new PopDataServiceClient();

daoWcf.GetSqlEmployeesDataCompleted += new

EventHandler(daoWcf_GetSqlEmployeesDataCompleted);

daoWcf.GetSqlEmployeesDataAsync(); break; default: break; } } }

着末一步是在page.xaml页面添加一个pop search form控件:

转自:http://blog.csdn.net/dujingjing1230/archive/2009/08/07/4423116.aspx

您可能还会对下面的文章感兴趣: