关于哪个WPF控件用于Dapper返回的数据结果的新手问题。 [英] Newbie question about which WPF controls to use for data result returned by Dapper.

查看:90
本文介绍了关于哪个WPF控件用于Dapper返回的数据结果的新手问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello:

我在SQL Server 2017数据库中有一些简单的数据:

USE [myDB]
GO

CREATE TABLE [dbo].[MatchEvents](
[EventID]    [int]              NOT NULL,
[Team1]      [nvarchar](40)     NOT NULL,
[Team2]      [nvarchar](40)     NOT NULL,
[TimeMatch]  [datetime]         NOT NULL,
CONSTRAINT PK_FutureCodeCN PRIMARY KEY(EventID]))
GO

INSERT INTO [MatchEvents] VALUES(1, 'Team A', 'Team B', '2018-05-23 13:00:00')
INSERT INTO [MatchEvents] VALUES(2, 'Team C', 'Team D', '2018-05-23 14:00:00')
INSERT INTO [MatchEvents] VALUES(3, 'Team E', 'Team F', '2018-05-23 15:00:00')

我有一些使用Dapper的C#代码获取所有数据:

using Dapper;

public struct MatchEvents
{
public int EventID { get; set; }
public string Team1 { get; set; }
public string Team2 { get; set; }
public DateTime TimeMatch { get; set; }
}

public static DbConnection GetOpenConnection()
{
var connection = new SqlConnection("Data Source=.;Initial Catalog=MyDB;Integrated Security=SSPI");
connection.Open();
return connection;
}

public static List<MatchEvents> get_events()
{
using (var connection = GetOpenConnection())
{
string Sql_Events = "SELECT EventID, Team1, Team2, TimeMatch FROM MatchEvents";
List<MatchEvents> events = (List<MatchEvents>)connection.Query<MatchEvents>(Sql_Events);
connection.Close();
return (events);
}
}

我想绑定
的结果
List< MatchEvents> 某些控件,但我不知道哪个控件是个不错的选择。我想执行以下操作:当程序
首次加载时,仅显示包括EventID,Team1,Team2和TimeMatch在内的所有信息的第一个事件(EventID = 1),此外,我想要一些控件(像一个按钮),所以当我点击控件(按钮)时,程序可以显示
下一个事件,直到最后一个事件,对于最后一个事件,如果用户点击控件(按钮)再次,显示一条消息,说明这是事件列表的结尾。

I我正在使用Blend for Visual Studio 2017,以及Visual Studio 2017(C#)

我想知道如何使用哪个控件或控件,最好向我展示XAML文件并给出一些解释。

谢谢,

推荐答案

嗨zydjohn,

Hi zydjohn,

根据你的描述,你从sql server数据库得到了列表< matchEvents>现在你要绑定这些数据来控制显示。我建议你可以使用DataGrid显示这些数据,你也想显示这些记录在
之一上,您可以查看以下代码:

According to your description, you have get list<matchEvents> from sql server database, now you want to bind this data to control to display.  I suggest you can use DataGrid to display these data, and you also want to display these record on by one, you can take a look the following code:

 public partial class Window10 : Window
    {
        public ObservableCollection<man> students { get; set; }
        public ObservableCollection<man> mans { get; set; }
        public int value=0;
        public Window10()
        {
            InitializeComponent();
            students = new ObservableCollection<man>()
            {
                new man(){Id=1,studentname="cherry",studentage=12},
                new man(){Id=2,studentname="barry",studentage=30},
                new man(){Id=3,studentname="mattew",studentage=12},
                new man(){Id=4,studentname="wendy",studentage=11},
                 new man(){Id=5,studentname="mattew1",studentage=12},
                  new man(){Id=6,studentname="mattew2",studentage=12},
                   new man(){Id=7,studentname="mattew3",studentage=12},
                    new man(){Id=8,studentname="mattew4",studentage=12},
                     new man(){Id=9,studentname="mattew5",studentage=12},
                      new man(){Id=10,studentname="mattew6",studentage=12},
                       new man(){Id=11,studentname="mattew7",studentage=12},
                        new man(){Id=12,studentname="mattew8",studentage=12},
                         new man(){Id=13,studentname="mattew9",studentage=12}
            };
            mans = new ObservableCollection<man>();
            mans.Add(students[0]);
            this.DataContext =mans;
        }

        private void btn1_Click(object sender, RoutedEventArgs e)
        {
            value+= 1;
            if(value>students.Count-1)
            {
                MessageBox.Show("last record has beed added!");
            }
            else
            {
                mans.Add(students[value]);               
            }
         
        }
    }
    public class man
    {
        public int Id { get; set; }
        public string studentname { get; set; }
        public int studentage { get; set; }
    }

我建议你使用Observablecollection<>存储数据,因为  ObservableCollection是给定类型的对象的动态集合。可以使用自动动作通知添加,删除或更新对象。当对象是
添加到可观察集合或从可观察集合中删除时,UI会自动更新。发生这种情况是因为,当绑定到可观察集合时,WPF会自动将一个CollectionChanged事件处理程序添加到ObservableCollecion的事件中。

I suggest you use Observablecollection<> to store data, because  ObservableCollection is a dynamic collection of objects of a given type. Objects can be added, removed or be updated with an automatic notification of actions. When an object is added to or removed from an observable collection, the UI is automatically updated. This happens because, when binding to an observable collection, WPF automatically adds a CollectionChanged event handler to the ObservableCollecion's events.

ObservableCollection类存在于中  System.Collections.ObjectModel   名称空间。

The ObservableCollection class exists in the System.Collections.ObjectModel namespace.

https://msdn.microsoft。 com / en-us / library / ms668604(v = vs.110).aspx

最好的问候,

Cherry


这篇关于关于哪个WPF控件用于Dapper返回的数据结果的新手问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆