开发者

How to bind EDM to WPF ListBox?

开发者 https://www.devze.com 2023-02-01 04:48 出处:网络
I\'m trying to figure out WPF binding to SQLite. I have an ADO.NET Entity Data Model generated to represent my SQLite database. The database only holds one table \"People\" with two columns \"person

I'm trying to figure out WPF binding to SQLite.

I have an ADO.NET Entity Data Model generated to represent my SQLite database. The database only holds one table "People" with two columns "person_id" and "person_name". Now, I generated EDM classes for that table within my WPF Application.

I'm trying to bind to a list box. I can delete items from source and see it updating the list box. But I can't add items to the source using a text box and see it update the list box.

I declared data entities in Window1 class like this:


private static MyNewSqliteDbEntities2 _myEntities = new MyNewSqliteDbEntities2();

I have a list box that is bound to the ObjectQuery in the Window_Loaded event handler like this:


private void Window_Loaded(object sender, RoutedEventArgs e)
{
    peopleListBox.ItemsSource = _myEntities.People;
}

I have another text box which I use to add people by clicking on button. And I can delete Items by selecting an item in the list box and clicking delete button. Changes are committed to the database when a commit button is clicked. Please consider the code below:


private void addButton_Click(object sender, RoutedEventArgs e)
{
    if (addPersonTextBox.Text != "")
    {
        People newPerson = new People();
        newPerson.person_name = addPersonTextBox.Text;
        //_myEntities.AddToPeople(newPerson);
        _myEntities.AddObject("People", newPerson);

        addPersonTextBox.Text = "";
    }
}

private void deleteButton_Click(object sender, RoutedEventArgs e)
{
    _myEntities.DeleteObject(peopleListBox.SelectedItem);
}

private void commitButton_Click(object sender, RoutedEventArgs e)
{
    _myEntities.SaveChanges();
}

I tried refreshing the list box control using another button called "Refresh" in the following manner but with no luck (although, when I step through the code I see the source is updated):


private void refreshButton_Click(object sender, RoutedEventArgs e开发者_开发知识库)
{
    peopleListBox.ItemsSource = null;
    peopleListBox.ItemsSource = _myEntities.People;
}

Here is the XAML code if you are wondering:


<Window x:Class="BindingToSqLite.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="400" Width="400" Loaded="Window_Loaded">
    <Window.Resources>
        <DataTemplate x:Key="personNameTemplate">
            <TextBlock Text="{Binding Path=person_name}"/>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="190*" />
            <ColumnDefinition Width="94*" />
            <ColumnDefinition Width="94*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="182*" />
            <RowDefinition Height="38*" />
            <RowDefinition Height="38*" />
            <RowDefinition Height="32*" />
        </Grid.RowDefinitions>
        <ListBox Margin="5" Name="peopleListBox" Grid.ColumnSpan="3" ItemTemplate="{StaticResource personNameTemplate}" />
        <TextBox Grid.Row="1" Grid.ColumnSpan="2" Margin="5,10" Name="addPersonTextBox" />
        <Button Grid.Column="2" Grid.Row="1" Margin="5" Name="addButton" Click="addButton_Click">Add</Button>
        <Button Grid.Row="2" Margin="5" Name="commitButton" Click="commitButton_Click">Commit</Button>
        <Button Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="2" Margin="5" Name="deleteButton" Click="deleteButton_Click">Delete</Button>
        <Button Grid.Row="3" Margin="5" Name="refreshButton" Click="refreshButton_Click">Refresh</Button>
    </Grid>
</Window>

I'm not sure if I'm doing this completely wrong. Any help is appreciated.


The People property needs to be an ObservableCollection<T> for this to work.

0

精彩评论

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

关注公众号