WPF应用程序,添加调度程序

时间:2014-10-09 18:07:40

标签: dispatcher

我必须每隔1秒添加一次代码来刷新我的应用程序。我还需要设置“新拍卖”按钮仅供管理员使用,按钮“出价”仅由用户设置。我已经创建了表用户(userID,userName,userPassword,userLevel)。 这就是我到目前为止所做的:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Auction" Height="350" Width="525"
    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
    >

<Grid DataContext="{Binding}">
    <DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="264" HorizontalAlignment="Left" ItemsSource="{Binding}" Name="aukcija_bazeDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="503" SelectionChanged="aukcija_bazeDataGrid_SelectionChanged">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="aukcijaIdColumn" Binding="{Binding Path=AukcijaID}" Header="Auction ID" Width="SizeToHeader" />
            <DataGridTextColumn x:Name="artikalNameColumn" Binding="{Binding Path=ProizvodIme}" Header="Item Name" Width="SizeToHeader" />
            <DataGridTextColumn x:Name="pocetnacenaColumn" Binding="{Binding Path=PocetnaCena}" Header="Start Price" Width="SizeToHeader" />
            <DataGridTextColumn x:Name="trenutnacenaColumn" Binding="{Binding Path=TrenutnaCena}" Header="Current Price" Width="SizeToHeader" />
        </DataGrid.Columns>
    </DataGrid>
    <Button Content="Login" Height="23" HorizontalAlignment="Left" Margin="331,276,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    <Button Content="New Auction" Height="23" HorizontalAlignment="Left" Margin="416,276,0,0" Name="button3" VerticalAlignment="Top" Width="75" Click="button3_Click" />
    <Button Content="Bid price" Height="23" HorizontalAlignment="Left" Margin="12,276,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
</Grid>

代码背后:

namespace WpfApplication1
{

public partial class MainWindow : Window
{

    DispatcherTimer timer;
    int interval;
    int step;

    public MainWindow()
    {
        InitializeComponent();

        interval = 1001;
        step = 20;
        timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromMilliseconds(interval);
        timer.Start();
        timer.Tick += new EventHandler(timer_Tick);

        DataTable aukcijeTable = new DataTable();
        SqlConnection conn = new SqlConnection(@"data source=JOVAN\SQLEXPRESS1;database=AukcijskaProdaja;integrated security=true;");
        SqlDataAdapter aukcDa = new SqlDataAdapter("select * from Aukcija", conn);

        aukcDa.Fill(aukcijeTable);
        aukcija_bazeDataGrid.DataContext = aukcijeTable;


    }



    void timer_Tick(object sender, EventArgs e)
    {
        if (Canvas.GetLeft(button1) > 400)
            timer.Stop();
        step = (step > 1) ? (step -= 1) : 1;
        Canvas.SetLeft(button1, Canvas.GetLeft(button1) + step);
        if (interval > 1)
            timer.Interval = TimeSpan.FromMilliseconds(interval -= 100);
    }

    private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = (@"data source=JOVAN\SQLEXPRESS1;database=AukcijskaProdaja;integrated security=true;");

            SqlCommand command = conn.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "uspPovecajCenuArtiklaZaJedan";
            command.Parameters.AddWithValue("@auction_ID", aukcija_bazeDataGrid.SelectedValue);
            conn.Open();
            command.ExecuteNonQuery();
        }
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Form popup = new Form();
        popup.ShowDialog();

        popup.Dispose();
    }

    private void button3_Click(object sender, RoutedEventArgs e)
    {
        Form popup = new Form();
        popup.ShowDialog();

        popup.Dispose();
    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = (@"data source=JOVAN\SQLEXPRESS1;database=AukcijskaProdaja;integrated security=true;");

            SqlCommand command = conn.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "uspPovecajCenuArtiklaZaJedan";
            command.Parameters.AddWithValue("@ArtikalID", 1);
            conn.Open();
            command.ExecuteNonQuery();
        }
    }

    private void loadGrid()
    {
        SqlConnection con = new SqlConnection(@"data source=JOVAN\SQLEXPRESS1;database=AukcijskaProdaja;integrated security=true;");
        SqlDataAdapter ad = new SqlDataAdapter();
        SqlCommand cmd = new SqlCommand();
        con.Open();
        string strQuery = "select * from Aukcija";
        cmd.CommandText = strQuery;
        ad.SelectCommand = cmd;
        cmd.Connection = con;
        DataSet ds = new DataSet();
        ad.Fill(ds);
        aukcija_bazeDataGrid.DataContext = ds.Tables[0].DefaultView;
        con.Close();
    }

    private void aukcija_bazeDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {


    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        DispatcherTimer dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
        dispatcherTimer.Start();
    }
    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {

    }
}

}  问题是我不知道在dispatcherTimer_Tick中写什么来使它工作。

0 个答案:

没有答案