WPF桌球计费实例

前端代码

<StackPanel>
    <WrapPanel>
        <Button x:Name="btnStart" Content="开台" Height="40" Width="80" Click="btnStart_Click"></Button>
        <Button x:Name="btnStop" Content="结账" Height="40" Width="80" Click="btnStop_Click"></Button>
    </WrapPanel>

    <DataGrid Name="lbStage" IsReadOnly="False" CanUserAddRows="False" CanUserDeleteRows="False" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="桌台编号" IsReadOnly="True" Width="100"  Binding="{Binding Path=id, Mode=TwoWay}"/>
            <DataGridTextColumn Header="计费方案" IsReadOnly="True" Width="100"  Binding="{Binding Path=name, Mode=TwoWay}"/>
            <DataGridTextColumn Header="开始时间" IsReadOnly="True" Width="180"  Binding="{Binding Path=starttime, Mode=TwoWay}"/>
            <DataGridTextColumn Header="累计时间" IsReadOnly="True"  Binding="{Binding Path=time, Mode=TwoWay}"/>
        </DataGrid.Columns>
    </DataGrid>
</StackPanel>

后台代码

public partial class UserWindow : Window
{
    System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
    System.Collections.ObjectModel.ObservableCollection<Stages> listStages = new System.Collections.ObjectModel.ObservableCollection<Stages>(){
    new Stages(){ id=1, name="花式台球"},
    new Stages(){ id=2, name="花式台球"},
    new Stages(){ id=3, name="美式台球"},
    new Stages(){ id=4, name="斯诺克"},
    new Stages(){ id=5, name="麻将"}
    };
    public UserWindow()
    {
        InitializeComponent();
        this.lbStage.ItemsSource = listStages;
        timer.Interval = new TimeSpan(0, 0, 1);
        timer.Tick += new EventHandler(TimerTick);
        timer.Start();
    }

    private void btnStart_Click(object sender, RoutedEventArgs e)
    {
        if (lbStage.SelectedItem != null)
        {
            Stages stage = lbStage.SelectedItem as Stages;
            if (stage.status == 0)
            {
                stage.status = 1;
                stage.starttime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                new System.Threading.Thread(WorkerProc) { IsBackground = true }.Start();
            }
            else
            {
                MessageBox.Show("您指定的台桌不是空台状态");
            }
        }
    }
    private void WorkerProc() { }
    private void btnStop_Click(object sender, RoutedEventArgs e)
    {
        if (lbStage.SelectedItem != null)
        {
            Stages stage = lbStage.SelectedItem as Stages;
            if (stage.status == 0)
            {
                MessageBox.Show("您指定的台桌是空台状态");
            }
            else
            {
                MessageBoxResult result = MessageBox.Show("确定结账台桌吗?", "温馨提示", MessageBoxButton.YesNo);
                if (result.ToString() == "Yes")
                {
                    stage.starttime = string.Empty;
                    stage.time = string.Empty;
                    stage.status = 0;
                    stage.minute = 0;
                }
            }
        }
    }
    private void TimerTick(object sender, EventArgs e)
    {
        foreach (Stages stage in listStages)
        {
            if (stage.status == 1)
            {
                stage.minute += 1;
                TimeSpan ts = new TimeSpan(0, 0, stage.minute);
                if (ts.Days > 0)
                    stage.time = string.Format("{0}天{1}小时{2}分钟{3}秒", ts.Days, ts.Hours, ts.Minutes, ts.Seconds);
                else if (ts.Hours > 0)
                    stage.time = string.Format("{0}小时{1}分钟{2}秒", ts.Hours, ts.Minutes, ts.Seconds);
                else
                    stage.time = string.Format("{0}分钟{1}秒", ts.Minutes, ts.Seconds);
            }
        }
    }
}
public class Stages : System.ComponentModel.INotifyPropertyChanged
{
    public int id { set; get; }
    public int _status;
    public int status
    {
        get { return _status; }
        set { _status = value; OnPropertyChanged("status"); }
    }
    public int minute { set; get; }
    public string name { set; get; }
    public string _time;
    public string time
    {
        get { return _time; }
        set { _time = value; OnPropertyChanged("time"); }
    }
    public string _starttime;
    public string starttime
    {
        get { return _starttime; }
        set { _starttime = value; OnPropertyChanged("starttime"); }
    }

    protected internal virtual void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
    }
    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
}

  源码下载:

posted @ 2021-03-28 04:46  microsoft-zhcn  阅读(93)  评论(0编辑  收藏  举报