C#Wpf Combobox设置selectedvalue,而不是在UI中更改selecteditem

时间:2018-04-14 13:14:16

标签: c# .net wpf combobox

我在usercontrol中有一个组合框

 <ComboBox DisplayMemberPath="CustomerCollection.Description1" SelectedValuePath="CustomerCollection.CustomerID" 
                      SelectedValue="{Binding Order.OrderCollection.CustomerID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                      ItemsSource="{Binding Customers, UpdateSourceTrigger=PropertyChanged}">
 </ComboBox>

我的视图模型有一个类模型Order,它有一个属性CustomerID,在视图模型中,我有一个observableCollection of customers

    private OrderModel _Order;
    public OrderModel Order
    {
        get
        {
            return _Order;
        }
        set
        {
            SetProperty(ref _Order, value);
        }
    }

    private ObservableCollection<CustomerModel> _Customers;
    public ObservableCollection<CustomerModel> Customers
    {
        get { return _Customers; }
        set { SetProperty(ref _Customers, value); }
    }

一切正常,但是当我设置Order.OrderCollection.CustomerID,所以我的组合框的SelectedValue,在ui中没有更新selecteditem

 private void ReloadCustomers(object CustomerID)
    {
        DialogVisibility = System.Windows.Visibility.Visible;

        BackgroundWorker bgwLoad = new BackgroundWorker();
        bgwLoad.DoWork += delegate (object s, DoWorkEventArgs args)
        {
            Customers = new ObservableCollection<CustomerModel>(_customerRepository.Get());
        };
        bgwLoad.RunWorkerCompleted += (sender, eventArgs) =>
        {
            Order.OrderCollection.CustomerID = (long)CustomerID;

            _eventAggregator.GetEvent<UIX_MessageEventAggregator.PassParameter>().Unsubscribe(ReloadCustomers);
        };
        bgwLoad.RunWorkerAsync();
    }

我尝试在Customer中实现equals的覆盖,但stil不工作,唯一的工作方式,似乎在viewmodel中创建了一个客户属性并绑定到selecteditem,但我不喜欢

       public override bool Equals(object obj)
    {
        var customer = obj as CustomerModel;

        if (customer != null)
            return customer.CustomerCollection.CustomerID == CustomerCollection.CustomerID;
        else
            return false;
    }

    public override int GetHashCode()
    {
        return CustomerCollection.CustomerID.GetHashCode();
    }

我知道我错过了什么,但我不明白什么? 有人有想法吗?

  public class OrderCollection : _CompanyModel
{
    public OrderCollection(string Vat) : base(Vat)
    {

    }

    #region Core
    private string _OrderID;
    public string OrderID
    {
        get { return _OrderID; }
        set { SetProperty(ref _OrderID, value); }
    }

    private long _CustomerID;
    public long CustomerID
    {
        get { return _CustomerID; }
        set { SetProperty(ref _CustomerID, value); }
    }
    #endregion
}

好的问题是BackgroundWorker 这项工作:

private void ReloadCustomers(object CustomerID)
{
   DialogVisibility = System.Windows.Visibility.Visible;

   Customers = new ObservableCollection<CustomerModel>(_customerRepository.Get());
   Order.OrderCollection.CustomerID = (long)CustomerID;
}

这不是:

private void ReloadCustomers(object CustomerID)
{
            DialogVisibility = System.Windows.Visibility.Visible;

            BackgroundWorker bgwLoad = new BackgroundWorker();
            bgwLoad.DoWork += delegate (object s, DoWorkEventArgs args)
            {
                Customers = new ObservableCollection<CustomerModel>(_customerRepository.Get());
            };
            bgwLoad.RunWorkerCompleted += (sender, eventArgs) =>
            {

                    Order.OrderCollection.CustomerID = (long)CustomerID;
                _eventAggregator.GetEvent<UIX_MessageEventAggregator.PassParameter>().Unsubscribe(ReloadCustomers);
            };
            bgwLoad.RunWorkerAsync();
        }

2 个答案:

答案 0 :(得分:0)

因为你没有发布OrderCollection类的内容,这只是一个假设。但我最好的猜测是,当您在PropertyChangedEvent中设置CustomerID属性时,您不会提出OrderCollection。 因此,View永远不会收到有关选择更改的通知。

因此,即使您的CustomID 更改模型实现以提升属性也应解决问题。
但是,您可能不希望使用此类事件污染您的模型。另一个选择是将模型(Customer,Order)包装在专用的ViewModel中,并在需要的地方引发事件。

答案 1 :(得分:0)

问题出在BackgroundWorker中,我已经补充了

      await Task.Run(() =>
        {
            Customers = new ObservableCollection<CustomerModel>(_customerRepository.Get());
        })
        .ContinueWith(t =>
        {
            Order.OrderCollection.CustomerID = (long)CustomerID;
            _eventAggregator.GetEvent<UIX_GlobalEvent.PassParameter>().Unsubscribe(ReloadCustomers);
        });