1

SQLite への WPF バインディングを理解しようとしています。

SQLite データベースを表すために生成された ADO.NET Entity Data Model があります。データベースには、「person_id」と「person_name」の 2 つの列を持つ「People」テーブルが 1 つだけ保持されます。ここで、WPF アプリケーション内でそのテーブルの EDM クラスを生成しました。

リストボックスにバインドしようとしています。ソースから項目を削除すると、リスト ボックスが更新されます。しかし、テキスト ボックスを使用してソースに項目を追加できず、リスト ボックスが更新されます。

次のように Window1 クラスでデータ エンティティを宣言しました。


private static MyNewSqliteDbEntities2 _myEntities = new MyNewSqliteDbEntities2();

次のように、Window_Loaded イベント ハンドラーで ObjectQuery にバインドされたリスト ボックスがあります。


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

ボタンをクリックして人を追加するために使用する別のテキストボックスがあります。また、リスト ボックスでアイテムを選択して削除ボタンをクリックすると、アイテムを削除できます。コミット ボタンをクリックすると、変更がデータベースにコミットされます。以下のコードを検討してください。


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();
}

次の方法で「更新」という別のボタンを使用してリスト ボックス コントロールを更新しようとしましたが、うまくいきませんでした (ただし、コードをステップ実行すると、ソースが更新されていることがわかります)。


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

ご参考までに、XAML コードを次に示します。


<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>

これが完全に間違っているかどうかはわかりません。どんな助けでも大歓迎です。

4

1 に答える 1

0

これが機能Peopleするには、プロパティが である必要がありますObservableCollection<T>

于 2010-12-28T09:12:30.097 に答える