2010-12-28 3 views
0

저는 SQLite에 대한 WPF 바인딩을 파악하려고합니다.EDM을 WPF ListBox에 바인딩하는 방법?

SQLite 데이터베이스를 나타 내기 위해 ADO.NET 엔터티 데이터 모델을 생성했습니다. 데이터베이스에는 두 개의 "person_id"및 "person_name"열이있는 "People"테이블이 하나만 있습니다. 이제는 WPF 응용 프로그램 내에서 해당 테이블에 대한 EDM 클래스를 생성했습니다.

목록 상자에 바인딩하려고합니다. 소스에서 항목을 삭제하고 목록 상자를 업데이트하는 것을 볼 수 있습니다. 하지만 텍스트 상자를 사용하여 소스에 항목을 추가하고 목록 상자를 업데이트하는 것을 볼 수는 없습니다.


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

내가 다른 텍스트 상자를 가지고 :

 

private static MyNewSqliteDbEntities2 _myEntities = new MyNewSqliteDbEntities2(); 
 
나는이 같은 Window_Loaded 이벤트 처리기에서하여 ObjectQuery에 바인딩 된 목록 상자가 있습니다

는이 같은 번째 윈도 클래스의 데이터 엔티티 선언 버튼을 클릭하여 사람을 추가하는 데 사용합니다. 그리고 목록 상자에서 항목을 선택하고 삭제 버튼을 클릭하여 항목을 삭제할 수 있습니다. 커밋 버튼을 클릭하면 변경 내용이 데이터베이스에 커밋됩니다.


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 패턴) 사이를 매핑해야 할 수도 있습니다. –