如何使用DataContext? [英] How to use DataContext?

查看:194
本文介绍了如何使用DataContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是简单地使用一个combobox填充来自​​sqlite表的数据。虽然我已经用代码方法,但我真的很想这样做,我可以看到更好的WPF的方式来做事情。

What I am trying to do is simply have a combobox populate with data from an sqlite table. While I have done this with code methods, I wold really like to do this in what I can see is the better WPF way to do things.

从我了解的流程应该像这样:

From what I understand the flow should go something like this:

我应该有一个持有数据的类,我做了一个快速类,默认构造函数是连接到数据库,并将其转储结果列表如下所示:

I should have a class that holds the data, I made a quick class which the default constructor is to connect to the database and dump it's results to a list like so:

internal class mainmenusql
{
    private List<string> _Jobs;

    public mainmenusql()
    {
        SQLiteConnection conn = new SQLiteConnection();
        conn.ConnectionString = "Data Source=C:\\Users\\user\\Documents\\db.sqlite;Version=3";

        try
        {
            conn.Open();
            SQLiteDataReader reader;
            SQLiteCommand command = new SQLiteCommand(conn);
            command.CommandType = CommandType.Text;
            command.CommandText = "SELECT * FROM Tasks";
            reader = command.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    _Jobs.Add(reader.GetValue(0).ToString());
                }
            }
            else
            {
                MessageBox.Show("No records");
            }

        }
        catch (Exception err)
        {
            MessageBox.Show(err.Message);
        }
        finally
        {
            conn.Close();
        }
    }
}

列表中有一些错误对象引用未设置为对象的实例。

Having some errors with the list "Object reference not set to an instance of an object".

但无论如何,下一步应该是设置 DataContext 这个对象的形式对吗?

But anyways, the next step should be to set the DataContext of the form to this object right?

    public MainWindow()
    {
        DataContext = new mainmenusql();
        InitializeComponent();
    }

最后,组合框应该有绑定权吗?

And finally the combobox should have a binding right?

<Window x:Class="sqliteDatacontext.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox DataContext="{Binding Path=_Jobs}" HorizontalAlignment="Left" Margin="141,124,0,0" VerticalAlignment="Top" Width="120"/>
    </Grid>
</Window>

我在这里做错什么?

推荐答案

对于绑定到数据文本的东西,需要通过public getter / setter来显示...

For binding to something of a datacontext, it needs to be exposed via public getter / setter...

public class mainmenusql
{
    public List<string> _Jobs
    { get ; protected set; }   


    public mainmenusql()
    {
       _Jobs = new List<string>();

       // rest of populating your data
    }
}

您的窗口控件中的绑定是ItemsSource

The binding in your window control is the ItemsSource

<ComboBox ItemsSource="{Binding Path=_Jobs}" />

DataContext应用于整个窗口...从那里作为基础,您的控件可以将其元素绑定到几乎任何公开可用的数据上下文中...在这种情况下,ComboBox的选择列表来自其ItemSource属性...所以您希望ITEMSOURCE指向您的_Jobs。

The "DataContext" is applied to the entire window... From that being the basis, any of your controls can have their element "bound" to almost anything "publicly" available ON your data context... in this case, a ComboBox list of choices comes from its "ItemSource" property... So you want the ITEMSOURCE pointing to your _Jobs.

这篇关于如何使用DataContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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