将组合框的选定项和项列表绑定到 wpf 中的不同属性

发布于 2024-08-16 19:24:35 字数 424 浏览 12 评论 0原文

问题:如何将 ComboBox 的选定项目的绑定设置为一个属性,并将项目列表设置为另一属性?


信息:

我正在为我的公司制定一个库存计划,该计划主要针对那些收到货物的人。我已经进行了一些设置,以便在屏幕的一侧有一个货物列表。当他们选择其中一项时,将显示货件的所有信息,以便可以对其进行编辑。一点信息是接收货物的人。我希望这样当他们单击发货时,显然收到发货的用户就是在组合框中弹出的用户。但我希望组合框包含所有其他用户的列表。

如果不是因为我要从数据库中提取用户列表,这并不难。列表框有一个数据模板,用于从用户数据类型列表中提取正确的信息。到目前为止我所尝试的是在组合框为其列表绑定的视图模型中拥有一个集合视图,然后是一个单独的属性,它是与发货数据类型一起提供的单个用户实例。

我根据 MV-VM 编程模型使用 Visual Basic 和 XAML 进行编程。

Question: How do you set the binding for a ComboBox's Selected items to one property and the items list to a different property?


Info:

I have an inventory program I'm working on for my company which is aimed mostly at the people who receive shipments as they come in. I have things set up so that there is a list of shipments on one side of the screen. when they select one of these all the information on the shipment is displayed so it can be edited. one bit of information is the person who received the shipment. I want it so that when they click the shipment, obviously the user who received the shipment is the one who pops up in the combo box. but i want the combo box to contain the list of all the other users.

This wouldn't be to hard if it weren't for the fact that I'm pulling the list of users from a database. the list box has a data template for pulling the proper information out of the list of user data types. what I've tried so far is to have a collection view in the viewmodel that the combo box binds to for its list, then a seperate property which is a single user instance that comes with the shipment data type.

I'm programing in Visual Basic and XAML according to the M-V-VM programing model.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

做个ˇ局外人 2024-08-23 19:24:35

你的做法没有任何问题。 ViewModel 可以公开来自数据库的类型集合以及绑定到所选项目的单独属性,例如:

public ObservableCollection<Shipment> AllShipments { get; private set; }
public Shipment SelectedShipment { get; set; }

// Constructor
public MyViewModel()
{
    AllShipments = ReadFromDb();
}

就组合框为空而言,

  • 启动应用程序时查看 VS 中的输出窗口 - 是否有一行抱怨绑定失败?
  • 根据 xaml 绑定仔细检查属性的拼写
  • 在 AllShipments getter 上放置一个断点,以确保在视图实例化绑定时它不为 null。如果您在虚拟机的构造函数中读取数据库(如上所述),那么就可以了。

另外 - 如果您希望组合框显示货件对象中的用户,请记住确保您的虚拟机(或其基础)实现了 INotifyPropertyChanged,并在 SelectedShipment 中引发 PropertyChanged 事件 setter - 这将通过 DataBinding 将更改传播到视图...

希望这有帮助:)

There is nothing wrong with your approach. The ViewModel can expose a collection of types from the db and a separate property bound to the selected item, something like:

public ObservableCollection<Shipment> AllShipments { get; private set; }
public Shipment SelectedShipment { get; set; }

// Constructor
public MyViewModel()
{
    AllShipments = ReadFromDb();
}

In terms of the combobox being empty

  • Have a look in the output window in VS when you start your app - is there a line complaining about a Binding failing?
  • Double-check the spellings of your properties against your xaml Binding
  • Put a breakpoint on the AllShipments getter to make sure it is not null when the View instantiates the binding. If you read the db in the VM's constructor (as above) then this will be OK.

Also - if you want the combo-box to display the User from the shipment object, then remember to ensure your VM (or its base) implements INotifyPropertyChanged, and raises a PropertyChanged event in the SelectedShipment setter - this will propagate the change through to the View via the DataBinding...

hope this helps :)

七分※倦醒 2024-08-23 19:24:35

假设您使用 Linq-to-SQL,我将回答这个问题:

这是一个常见的 PITA 问题,其中 Shipment 对象中的 User 属性包含一个引用到与填充 ComboBox 的查询中的 User 对象不同的 User 对象,即使它们代表相同的记录。

为了克服这个问题,您可能需要将用户 ComboBox 绑定到从与 Shipments 列表相同的 DataContext 检索的用户列表。 。

例子:

class ViewModel
{
    public List<User> Users { get; set; }
    public List<Shipment> Shipments { get; set; }
    public Shipment SelectedShipment { get; set; }

    public void ReadShipments()
    {
        using (var dc = new MyDataContext())
        {
            DataLoadOptions dlo = new DataLoadOptions();
            dlo.LoadWith<Shipment>(x => x.User);
            dc.LoadOptions = dlo;
            Shipments = dc.Shipments.ToList();
            Users = dc.Users.ToList();
        }
    }
}

I'll be answering this question assuming you're using Linq-to-SQL:

This is a common PITA problem, where the User property in your Shipment object contains a reference to a User object that is different from the User object from the query that populated the ComboBox, even though they represent the same record.

To overcome this, you might want to bind the users ComboBox to a list of users that is retrieved from the same DataContext as the list of Shipments.

Example:

class ViewModel
{
    public List<User> Users { get; set; }
    public List<Shipment> Shipments { get; set; }
    public Shipment SelectedShipment { get; set; }

    public void ReadShipments()
    {
        using (var dc = new MyDataContext())
        {
            DataLoadOptions dlo = new DataLoadOptions();
            dlo.LoadWith<Shipment>(x => x.User);
            dc.LoadOptions = dlo;
            Shipments = dc.Shipments.ToList();
            Users = dc.Users.ToList();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文