我在MainWindow.xaml上放置了两个DatePicker控件,如下所示:
<DatePicker Name="dpStartDate"
SelectedDate="{Binding Path=StartDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
DisplayDateStart="1/1/2018"
FirstDayOfWeek="Sunday"/>
<Label Name="lblEndDate" Content ="End Date:"/>
<DatePicker Name="dpEndDate"
SelectedDate = "{Binding Path = EndDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
DisplayDateStart="1/1/2018"
FirstDayOfWeek="Sunday"/>
我的目的是让所有条目用户控件查询后端数据(本例中为Quickbooks),并返回与MainWindow DatePicker控件中的开始和结束日期值匹配的记录。
AllEntries类如下:
private void EntriesDataGrid_OnLoaded(object sender, RoutedEventArgs e)
{
bool sessionBegun = false;
bool connectionOpen = false;
DatePicker dpStartDate;
DatePicker dpEndDate;
QBSessionManager sessionManager = null;
try
{
//Create Session Manager
sessionManager = new QBSessionManager();
//Create the request to obtain the Profit and Loss Summary Report
IMsgSetRequest requestMsgSet = sessionManager.CreateMsgSetRequest("US", 8, 0);
requestMsgSet.Attributes.OnError = ENRqOnError.roeContinue;
//Connect to QB Desktop and begin a Session
sessionManager.OpenConnection(
@"C:\Users\Public\Public Documents\Intuit\QuickBooks\Company Files\MyCompany.QBW",
"MyCompany");
connectionOpen = true;
sessionManager.BeginSession("", ENOpenMode.omDontCare);
sessionBegun = true;
IGeneralDetailReportQuery plQuery = requestMsgSet.AppendGeneralDetailReportQueryRq();
plQuery.GeneralDetailReportType.SetValue(ENGeneralDetailReportType.gdrtTxnListByDate);
plQuery.DisplayReport.SetValue(true);
//TODO: Set FromReportDate and ToReportDate equal to plStartDate.SelectedDate and plEndDate.SelectedDate
//plQuery.ORReportPeriod.ReportPeriod.FromReportDate = ;
//plQuery.ORReportPeriod.ReportPeriod.ToReportDate = ;
IMsgSetResponse responseMsgSet = sessionManager.DoRequests(requestMsgSet);
IResponse response = responseMsgSet.ResponseList.GetAt(0);
IORReportData reportData = (IORReportData)response.Detail;
//TODO: Create list of report items building from model class(es).
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
finally
{
if (sessionBegun)
{
sessionManager.EndSession();
}
if (connectionOpen)
{
sessionManager.CloseConnection();
}
}
}
如何绑定MainWindow中的DatePicker值并将它们用作查询请求的变量?
答案 0 :(得分:1)
为UserControl提供类型为DateTime?
的两个公共依赖项属性。将这些属性绑定到绑定中带有ElementName
的日期选择器的值。
我认为您希望在依赖项属性值更改时更新AllEntries
,而不是仅在OnLoaded()
中更新。我强烈建议您使用OnLoaded()
的内容并将其全部放在另一个名为RunQuery()
的方法中,然后从OnLoaded()
,依赖属性更改处理程序或任何位置调用该方法。