2010-12-28 13 views
0

私はSQLiteへのWPFバインディングを理解しようとしています。EDMをWPF ListBoxにバインドする方法は?

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

私はリストボックスにバインドしようとしています。ソースから項目を削除し、リストボックスを更新することができます。しかし、私はテキストボックスを使用してソースに項目を追加することはできませんし、リストボックスを更新する参照してください。

私はこのようなウィンドウ1クラスのデータ・エンティティを宣言:

 

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

:以下のコードを考えてみてください。


&ltWindow 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"> 
    &ltWindow.Resources> 
     &ltDataTemplate x:Key="personNameTemplate"> 
      &ltTextBlock Text="{Binding Path=person_name}"/> 
     </DataTemplate> 
    </Window.Resources> 
    &ltGrid> 
     &ltGrid.ColumnDefinitions> 
      &ltColumnDefinition Width="190*" /> 
      &ltColumnDefinition Width="94*" /> 
      &ltColumnDefinition Width="94*" /> 
     </Grid.ColumnDefinitions> 
     &ltGrid.RowDefinitions> 
      &ltRowDefinition Height="182*" /> 
      &ltRowDefinition Height="38*" /> 
      &ltRowDefinition Height="38*" /> 
      &ltRowDefinition Height="32*" /> 
     </Grid.RowDefinitions> 
     &ltListBox Margin="5" Name="peopleListBox" Grid.ColumnSpan="3" ItemTemplate="{StaticResource personNameTemplate}" /> 
     &ltTextBox Grid.Row="1" Grid.ColumnSpan="2" Margin="5,10" Name="addPersonTextBox" /> 
     &ltButton Grid.Column="2" Grid.Row="1" Margin="5" Name="addButton" Click="addButton_Click"&gtAdd</Button> 
     &ltButton Grid.Row="2" Margin="5" Name="commitButton" Click="commitButton_Click"&gtCommit</Button> 
     &ltButton Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="2" Margin="5" Name="deleteButton" Click="deleteButton_Click"&gtDelete</Button> 
     &ltButton Grid.Row="3" Margin="5" Name="refreshButton" Click="refreshButton_Click"&gtRefresh</Button> 
    </Grid> 
</Window> 

私は、これは完全に間違ってやっているのかはわからない:):あなたが思っている場合はここ


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

は、XAMLコードです。どんな助けもありがとうございます。

答えて

0

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

+0

はい、ADO.net EDMはObservableCollection を生成しません。 – mbadawi23

+0

@ mbadawi23の場合は、ビューモデルを使用し、EDMモデルとビューモデル(MVVMパターン)の間でマッピングする必要があります。 –