简体   繁体   中英

How to Bind WPF DatePicker in MainWindow for Use in a UserControl?

I've placed two DatePicker controls on my MainWindow.xaml as follows:

<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"/>

My intent is for the All Entries User Control to query the backend data (Quickbooks in this case) and return the records matching the Start and End Date values from the MainWindow DatePicker controls.

The AllEntries class is as follows:

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();
            }
        }
    }

How do I bind the DatePicker values from MainWindow and use them as variables for my query request?

Give the UserControl two public dependency properties of type DateTime? . Bind those properties to the values of the date pickers with ElementName in the bindings.

I think you'll want to update AllEntries when the dependency property values change, rather than only in OnLoaded() . I urge you to take the guts of OnLoaded() and put it all in another method called RunQuery() , then call that method from OnLoaded() , from the dependency property changed handler, or wherever.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM