MVVMパターンを使用するWPFアプリケーションがあります。ComboBoxデータを外部キーデータとリンクすると、ComboBoxから間違ったインデックスが使用されます
私は2つのテーブル
顧客を持っている:CurrencyId、説明
は、両方のテーブルは主キーに自動インクリメントを使用して、上の外国人の関係がある:のCustomerId、CustomerNameのは、
通貨CurrencyId 2つのCurrencyId列。
いくつかの苦労した後、私は顧客のリストを表示し、それぞれをクリックすることができ、データはバインドされていますが、インデックスは正しくありません。
通貨テーブルの増分は1から始まりますが、ComboBoxは0から開始されるため、基本的に顧客ごとに不適切な通貨の説明が表示されます。
必要な場合はわかりませんが、これまでのところ私のコードです。
のViewModel:
public class TestingViewModel : ViewModelBase
{
// Constructor
public TestingViewModel()
{
Customers = GetCustomers();
Currencies = GetCurrencies();
}
private ObservableCollection<Customer> _customers;
public ObservableCollection<Customer> Customers
{
get { return _customers; }
set
{
_customers = value;
RaisePropertyChanged("Customers");
}
}
private ObservableCollection<Currency> _currencies;
public ObservableCollection<Currency> Currencies
{
get { return _currencies; }
set
{
_currencies = value;
RaisePropertyChanged("Currencies");
}
}
private ObservableCollection<Customer> GetCustomers()
{
var dbContext = new DbDataContext();
return new ObservableCollection<Customer> dbContext.Customers.ToList());
}
private ObservableCollection<Currency> GetCurrencies()
{
var dbContext = new DbDataContext();
return new ObservableCollection<Currency>(dbContext.Currencies.ToList());
}
}
ビュー:
<Grid>
<ListView x:Name="LstCustomers" Grid.Column="0" Grid.Row="1"
ItemsSource="{Binding Path=Customers, Mode=Oneway}" IsSynchronizedWithCurrentItem="True" Background="White"
ItemContainerStyle="{StaticResource ListViewItemsStyle}">
<ListView.ItemTemplate>
<DataTemplate >
<TextBlock Text="{Binding Path=CustomerName, Mode=OneTime}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<TextBox x:Name="TbContactName" Width="300" Height="30" Margin="0,-100,0,0"
VerticalAlignment="Center" HorizontalAlignment="Right"
Text="{Binding ElementName=LstCustomers, Path=SelectedItem.ContactName, UpdateSourceTrigger=PropertyChanged}" />
<ComboBox x:Name="combobox" ItemsSource="{Binding Currencies}" Width="300" Height="40" HorizontalAlignment="Right"
DisplayMemberPath="Description"
SelectedValuePath="CurrencyId"
SelectedIndex="{Binding ElementName=LstCustomers, Path=SelectedItem.CurrencyId}" />
</Grid>
は私のコードで間違ったものがあるか、そうでない場合はどのように私はこの問題を解決することができますか?あなたはコンボボックスが間違っているそのCurrencyId、同じであることに通貨の指標に依存している
それは素晴らしいですが、私は通貨の変更をどのように保存しますか? – Neill
UIから通貨を選択するたびにSelectedCurrencyを設定する必要があります。私はあなたのプロパティセッターにコードを追加できると思います。 – shwick
ええ、それをして、素晴らしい作品です。ありがとう。 – Neill