从ListBox中的SelectedItem对象绑定到ComboBox

时间:2013-07-03 00:46:27

标签: mvvm binding combobox entity master-detail

以下是我的UI的一部分: http://imgur.com/MtXSCnL

在Department List中,我有一个Department对象列表(我正在使用MVVM模式)。我通过 DepartmentViewModel 中的Observable集合访问这些对象。

我正在使用Entity Framework链接到我的数据库,我正在尝试执行以下操作:

我绑定了部门列表中的 SelectedItem ,以显示部门组框中的信息。当我尝试将部门的外键(在本例中为Office)绑定到Combobox时会出现问题。我在部门视图模型中有 Office 类型的 Office 属性但是我想在组合框中显示Office对象的OfficeName属性,并且能够修改和保存更改到部门(这个的逻辑已经完成)。

如何绑定 Office 类型的属性以在组合框中显示 OfficeName

DepartmentViewModel:

   /// <summary>
    /// Office
    /// </summary>
    private const string OfficePropertyName = "Office";
    public Office Office
    {
        get { return _newDepartment.Office; }
        set
        {
            _newDepartment.Office = value;
            OnPropertyChanged(OfficePropertyName);
        }
    }

    private ObservableCollection<Office> _OfficeList;

    private const string OfficeListPropertyName = "OfficeList";
    public ObservableCollection<Office> OfficeList
    {
        get
        {
            return _OfficeList;
        }
        set
        {
            _OfficeList = value;
            OnPropertyChanged(OfficeListPropertyName);
        }
    }

DepartmentView

            <ListBox Grid.Row="0"
                 ItemsSource="{Binding DepartmentList}" 
                 DisplayMemberPath="DepartmentName"
                 SelectedItem="{Binding SelectedDepartment}"/>


            <StackPanel Grid.Row="1" Grid.Column="2">
                <TextBox Margin="10,10" Text="{Binding SelectedDepartment.DepartmentName}" />
                <TextBox Margin="10,10" Text="{Binding SelectedDepartment.Supervisor}" />

                <!-- List of all Office...here is where I want to show the selected office from the selected item in department list -->
                <ComboBox Grid.Row="3" Grid.Column="1" Margin="10,10"
              ItemsSource="{Binding OfficeList}"
              DisplayMemberPath="OfficeName"
              SelectedItem="{Binding Office}" />

            </StackPanel>

0 个答案:

没有答案