私は2つのテーブルを持つデータベースを持っています。 tblContact
およびtblPerson
。 tblContact
とtblPerson
の間に1対多の関係があります.1人の連絡先には多くの人がいる場合があります。私はデータバインディングを作成するためにVB 2013とEntity Frameworkを使用しています。バインドされた子オブジェクトのComboBox ItemSourceの更新
バインドされたComboBox(cboPerson
)の問題は、別の連絡先に移動してから戻るまで人を追加/削除/変更すると更新されません。 ComboBox ItemSourceをリフレッシュするメソッドはありますか?
私が行った変更は、基礎となるオブジェクトとデータベースに反映されていることがわかります。
ここにXAMLがあります。
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:TestBinding" mc:Ignorable="d" x:Class="MainWindow"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<CollectionViewSource x:Key="ContactView" d:DesignSource="{d:DesignInstance {x:Type local:tblContact}, CreateList=True}"/>
<CollectionViewSource x:Key="PersonView" Source="{Binding tblPersons, Source={StaticResource ContactView}}" />
</Window.Resources>
<Grid DataContext="{Binding Source={StaticResource ContactView}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Grid.Row="0" Margin="3" Grid.Column="0" Content="Contact ID:"/>
<TextBox Grid.Row="0" Margin="3" Grid.Column="1" Padding="3" x:Name="txtContactID" VerticalAlignment="Center" Text="{Binding Contact_ID, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" />
<Label Grid.Row="1" Grid.Column="0" Margin="3" Content="Person ID:" />
<ComboBox Grid.Row="1" Grid.Column="1" Margin="3" Padding="3" x:Name="cboPerson" SelectedValue="{Binding Contact_Primary_Person}" ItemsSource="{Binding Source={StaticResource ContactView}, Path=tblPersons, Mode=TwoWay}" SelectedValuePath="Person_ID" DisplayMemberPath="Person_Salutation" />
<StackPanel Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Orientation="Horizontal" >
<Button Margin="3" Padding="3" Name="btnAdd">Add</Button>
<Button Margin="3" Padding="3" Name="btnPrev">Prev</Button>
<Button Margin="3" Padding="3" Name="btnNext">Next</Button>
<Button Margin="3" Padding="3" Name="btnDelete">Delete</Button>
</StackPanel>
</Grid>
</Window>
フォームの背後にあるコード。
アドバイスや説明をいただければ幸いです。
コードを少なく掲示した方が役に立ちます。問題に関連するコードだけが、クラス全体を辿るのではなく、これをもっと簡単に答えることができます。 – David