开发者

Bind ObservableCollection to database

开发者 https://www.devze.com 2023-03-28 15:38 出处:网络
I have the following class: [Table] public class myClass { [Column(IsPrimaryKey = true)] public int ID { get; set; }

I have the following class:

[Table]
public class myClass
{
    [Column(IsPrimaryKey = true)]
    public int ID { get; set; }

    [Column]
    public string Type { get; set; }
}

And the following code in order to load the data from my database table "myClass":

static public readonly ObservableCollection<myClass> items;
DataContext dat开发者_如何学PythonaContext = new DataContext("Secret");

var ruleTable = dataContext.GetTable<myClass>();

IQueryable<Rule> custQuery = ruleTable.Select(tableName => tableName);
items = new ObservableCollection<myClass>(custQuery);

How can I make the database update based on my ObservableCollection?


Bind the items to a ListBox with an ItemTemplate or any other ItemsControl.

<ListBox x:Name="myListBox">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Content="{Binding ID}"/>
                <TextBox Content="{Binding Type, Mode=TwoWay}"/>
            </StackPanel>
        </DataTemplate>
    <ListBox.ItemTemplate>
</ListBox>

Set the datacontext in code behind:

myListBox.DataContext = items;

Then add a button with an event that executes dataContext.SubmitChanges


This codeproject article I wrote a while back has a section that details performing database update / delete / insert operations from a bound WPF datagrid:

WPF DataGrid Practical Examples

0

精彩评论

暂无评论...
验证码 换一张
取 消