使用Windows应用商店中的c#从DatePicker获取日期

时间:2014-08-18 07:47:32

标签: c# xaml datepicker windows-store-apps windows-8.1

我想在我的数据库中插入日期值我有一个具有日期时间属性的类

public DateTime BookingDate { get; set; }

我的xaml页面中有一个名为datepicker的日期选择器。我想只插入日期值,如何使用c#??

获取日期

2 个答案:

答案 0 :(得分:2)

您可以使用Date属性,请按照文档

进行操作
  DateTimeOffset sourceTime = YourDatePicker.Date;
  BookingDate = sourceTime.DateTime;

转换回偏移并绑定到datetimepicker

   DateTime newBookingDate;
   newBookingDate = DateTime.SpecifyKind(BookingDate, DateTimeKind.Utc);
   DateTimeOffset bindTime = newBookingDate;
   YourDatePicker.Date = bindTime;

Adding a DatePicker (XAML)

答案 1 :(得分:0)

使用MVVM模式:

查看(XAML):

import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Spinners implements ChangeListener {

    private int currentTotal = 15;
    private final int maxTotal = 20;
    private final JSpinner[] spins = new JSpinner[3];

    public Spinners() {

        JFrame frame = new JFrame();
        JPanel content = new JPanel(new GridLayout(3, 1));

        for (int i = 0; i < 3; i++) {

            SpinnerModel model = new SpinnerNumberModel(5, //initial value
                    1, //min
                    10, //max
                    1); //step
            JSpinner spinner = new AutoBlockSpinner();
            spinner.setModel(model);
            spinner.addChangeListener(this);
            spins[i] = spinner;
            content.add(spinner);
        }

        computeCurrentTotal();

        frame.setContentPane(content);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();

    }

    private void computeCurrentTotal() {

        currentTotal = 0;

        for (JSpinner spin : spins) {

            currentTotal += (Integer) spin.getModel().getValue();
        }

    }

    @Override
    public void stateChanged(final ChangeEvent e) {

        computeCurrentTotal();

    }

    public static void main(final String[] args) {

        Spinners spinners = new Spinners();

    }

    class AutoBlockSpinner extends JSpinner {

        @Override
        public Object getNextValue() {

            if (currentTotal + 1 <= maxTotal) {
                return super.getNextValue();
            } else {
                return null;
            }

        }

    }

}

ViewModel(C#):

<DatePicker Date="{Binding DateOffset, Mode=TwoWay}" />