WPF 组合框实体框架绑定 (MVVM)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17269021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:03:42  来源:igfitidea点击:

WPF Combobox Entity Framework Binding (MVVM)

wpfmvvmbindingcombobox

提问by francisg3

I am using the EntityFramework library in my WPF Application and I am having the following issue: I am using the MVVM pattern (to the best of my knowledge) and I am trying to make a Combobox Lookup with EF values.

我在 WPF 应用程序中使用 EntityFramework 库,但遇到以下问题:我正在使用 MVVM 模式(据我所知),我正在尝试使用 EF 值进行组合框查找。

  • I have a Company class which contains many Offices (a class as well)
  • This has been modelled through the EntityFramework and all the links are correct (Office has a CompanyName which is a foreign key).
  • 我有一个包含许多办公室的公司类(也是一个类)
  • 这是通过 EntityFramework 建模的,并且所有链接都是正确的(Office 有一个 CompanyName,它是一个外键)。

Here is the OfficeView class:

这是 OfficeView 类:

public partial class AddOffice : Window
{
    private DBHelper.ResourceManagementContext context = new DBHelper.ResourceManagementContext();
    public AddOffice()
    {
        InitializeComponent();
        context.Companies.Load(); 
        this.DataContext = context.Companies.Local; 
        //this.DataContext = new AddOfficeViewModel();
    }

    public void CloseCommandHandler(object sender, ExecutedRoutedEventArgs e)
    {
        this.Close();
    }
}

Here is the corresponding XAML:

这是相应的 XAML:

  <Label Grid.Row="4" Grid.Column="0" Margin="10,10">Company:</Label>
    <ComboBox Grid.Row="4" Grid.Column="1" Margin="10,10"
              ItemsSource="{Binding}"
              DisplayMemberPath="CompanyName"
              SelectedValuePath="CompanyName"
              SelectedValue="{Binding Path=CompanyName}"/>

I know the MVVM pattern usually passes a ViewModel to the View so how would I accomplish binding the EntityFramework Company list to the ComboBox using the OfficeViewModel?

我知道 MVVM 模式通常将 ViewModel 传递给 View,那么我将如何使用 OfficeViewModel 将 EntityFramework Company 列表绑定到 ComboBox?

I understand the ComboBox properties. I know the selected value would be the CompanyName from the Office object and the SeletecValuePath would be the CompanyName from the Company object.

我了解 ComboBox 属性。我知道所选值将是 Office 对象中的 CompanyName,SeletecValuePath 将是 Company 对象中的 CompanyName。

回答by Krish

In View Model:

在视图模型中:

    class OfficeViewModel
{

    private string _CompanyName;

    public string CompanyName
    {
        get
        {
            return _CompanyName;
        }
        set
        {
            _CompanyName = value;
            NotifyPropertyChanged("CompanyName");
        }
    }

    private List<Location> _CompanyList;

    public List<Location> CompanyList
    {
        get
        {
            return _CompanyList;
        }
        set
        {
            _CompanyList = value;
            NotifyPropertyChanged("CompanyList");
        }
    }

    public List<Company> GetCompanyList()
    {
        return (from comp in Entity.Companies select comp).ToList(); 
    }
}

In Xaml:

在 Xml 中:

Add the namespace in xaml as follows:

在 xaml 中添加命名空间如下:

xmlns:ViewModels="clr-namespace:WpfMvvmApplication.ViewModels"

Add the following in window.resources:

在 window.resources 中添加以下内容:

<Window.Resources>
    <ViewModels:OfficeViewModel x:Key="OfficeController"/>       
</Window.Resources>

Bind the View Model to combobox:

将视图模型绑定到组合框:

 <Label Grid.Row="4" Grid.Column="0" Margin="10,10">Company:</Label>
 <ComboBox Grid.Row="4" Grid.Column="1" Margin="10,10"
          ItemsSource="{Binding CompanyList, Source={StaticResource OfficeController}}"
          DisplayMemberPath="CompanyName"
          SelectedValuePath="CompanyName"
          SelectedValue="{Binding Path=CompanyName}"/>

Hope this helps you.

希望这对你有帮助。