简体   繁体   English

WPF和ADO.NET上的项目

[英]Project on WPF and ADO.NET

I have a couple of problems in a project I need to make. 我需要做的一个项目中有几个问题。

  1. Project has to have listed all the table data(auctions) in a listbox and when a user wants to bid for a certain bid he can select the bid from the listbox and click Bid button. 项目必须在列表框中列出所有表格数据(拍卖),并且当用户要为某个出价出价时,他可以从列表框中选择出价,然后单击出价按钮。
  2. When the Bid button is pressed it needs to increase value of the bid by one and it needs to write the name of the user who is biding. 按下“出价”按钮时,它需要将出价的值增加一,并且需要写出要出价的用户的姓名。
  3. I have created update statements in bid button event, but it doesnt work properly, it just stays as it is with no change. 我已经在出价按钮事件中创建了更新语句,但是它不能正常工作,它保持不变。

    Also how can I implement that the bid button works only on selected item in a listbox ? 另外,如何实现出价按钮仅在列表框中的选定项目上起作用?

My code: 我的代码:

public MainWindow()
{
  InitializeComponent();
  tmr.Interval = new TimeSpan(0, 0, 1);
  tmr.Tick += tmr_Tick;
  showData sd = new showData();
  sd.ToString();
  listBox1.Items.Add(sd.ToString());

}


private void btn_Click(object sender, RoutedEventArgs e)
{
  string user = "test";
  int a = +1 ;
  using (SqlConnection conn = new SqlConnection(@" Data source=ALASAD; Initial Catalog=aukcija_Arsen_Milosev; Integrated Security = true;"))
  {
    conn.Open();
    using (SqlCommand comm = new SqlCommand("UPDATE auctions SET bidValue = @bidvalue, lastBider = @lastbider",conn))
    {
      comm.Parameters.AddWithValue("@lastbider", user);
      comm.Parameters.AddWithValue("@bidValue", a);
      comm.ExecuteNonQuery();
      conn.Close();
    }

  }
  listBox1.Items.Clear();
  showData sd = new showData();
  sd.ToString();
  listBox1.Items.Add(sd.ToString());
}

Following MVVM pattern you should expose in the ViewModel properties for User and SelectedAuction. 按照MVVM模式,您应该在User和SelectedAuction的ViewModel属性中公开。 And bind the properties to ListBox in XAML 并将属性绑定到XAML中的ListBox

XAML would be like this "ListBox Items={Binding Auctions} SelectedItem={Binding Auction} />" XAML就像这样“ ListBox Items = {Binding Auctions} SelectedItem = {{Binding Auction} />””
"Button Command={Binding Bid} />" “按钮命令= {绑定出价} />”

And ViewModel 和ViewModel

using MicroMvvm;

public AuctionsViewModel : ObservableObject
{
    private string user;
    private int auction;
    private ObservableCollection<int> auctions;

    public string User
    {
        get{return user;}
        set         
        {
            if(user!=value)
            {
                user=value;
                RaisePropertyChanged("User");
            }               
        }
    }

    public int Auction
    {
        get{return auction;}
        set         
        {
            if(auction!=value)
            {
                auction=value;
                RaisePropertyChanged("Auction");
            }               
        }
    }

    public ObservableCollection<int> Auctions
    {
        get{return auctions;}
        set         
        {
            if(auctions!=value)
            {
                auctions=value;
                RaisePropertyChanged("Auctions");
            }               
        }
    }

    public ICommand Bid
    {
        get{return new RelayCommand(BidExecute);}
    }

    private void BidExecute()
    {
        Auction++;
        using (SqlConnection conn = new SqlConnection(@" Data source=ALASAD; Initial Catalog=aukcija_Arsen_Milosev; Integrated Security = true;"))
        {
            conn.Open();
            using (SqlCommand comm = new SqlCommand("UPDATE auctions SET bidValue = @bidvalue, lastBider = @lastbider",conn))
            {
                comm.Parameters.AddWithValue("@lastbider", User);
                comm.Parameters.AddWithValue("@bidValue", Auction);
                comm.ExecuteNonQuery();
                conn.Close();
            }
        }
        Auctions.clear();           
        showData sd = new showData();
        Auctions.AddRange(sd.toString());
        RaisePropertyChanged("Auctions");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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