将ComboBox数据与外键数据链接,ComboBox

时间:2016-09-16 11:31:00

标签: wpf mvvm data-binding

我有一个使用MVVM模式的WPF应用程序。

我有两张桌子

客户:CustomerId,CustomerName,CurrencyId

货币:CurrencyId,描述

两个表都在主键上使用自动递增,并且两个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>

我的代码中是否有不正确的内容,或者我该如何解决?

谢谢,

1 个答案:

答案 0 :(得分:1)

您依赖于ComboBox中Currency的索引与其CurrencyId相同,这是错误的 - CurrencyId是您数据库中的ID,ComboBox中Currency元素的索引取决于它们在ComboBox中的顺序。

我会在ViewModel中添加SelectedCustomer和SelectedCurrency属性,并从SelectedCustomer属性setter更新SelectedCurrency。例如:

private Customer _selectedCustomer;
public Customer SelectedCustomer
{
    get { return _selectedCustomer; }
    set
    {
        _selectedCustomer = value;
        RaisePropertyChanged("SelectedCustomer");
        SelectedCurrency = this.Currencies
            .FirstOrDefault(x => x.CurrencyId == SelectedCustomer.CurrencyId);
    }
}

private Currency _selectedCurrency;
public Currency SelectedCurrency
{
    get { return _selectedCurrency; }
    set
    {
        _selectedCurrency = value;
        RaisePropertyChanged("SelectedCurrency");
    }
}

然后,不要像SelectedIndex="{Binding ElementName=LstCustomers, Path=SelectedItem.CurrencyId}"那样绑定它,而是像SelectedItem = {Binding SelectedCurrency}那样绑定它,并对SelectedCustomer执行相同操作。