저는 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();
}
내가 코드를 단계별로 할 때 내가 볼,하지만 소스가 업데이트됩니다 (다음과 같은 방법으로하지만 행운 "새로 고침"라는 또 다른 버튼을 사용하여 목록 상자 컨트롤을 새로 고침 시도 : 아래의 코드를 고려하시기 바랍니다) :
<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>
나는이 완전히 잘못하고 있어요 만약 내가 확실하지 않다 :
private void refreshButton_Click(object sender, RoutedEventArgs e)
{
peopleListBox.ItemsSource = null;
peopleListBox.ItemsSource = _myEntities.People;
}
다음은 궁금해하는 경우 XAML 코드입니다. 어떤 도움을 주셔서 감사합니다.
예,하지만 ADO.net EDM은 ObservableCollection을 생성하지 않습니다. –
mbadawi23
@ mbadawi23의 경우이 경우보기 모델을 사용하고 EDM 모델과보기 모델 (MVVM 패턴) 사이를 매핑해야 할 수도 있습니다. –