【问题标题】:Unable to get the binding from textbox and bind combobox from another view model无法从文本框获取绑定并从另一个视图模型绑定组合框
【发布时间】:2015-05-07 18:31:49
【问题描述】:

我对 mvvm 非常陌生,所以请耐心等待。我有 2 个继承的视图模型,即 DBViewModel 和 PersonViewModel。我想在 DBViewModel 中添加人员对象,并将 2 组合框与 PersonViewModel 中的 observablecollection 绑定。

 public class PersonViewModel
{
    private ICommand AddCommand ;

    public Person PersonI{get;set;}
    public ObservableCollection<Person> EmployeeList{ get; set; }
    public ObservableCollection<String> OccupationList{ get; set; }

    public PersonViewModel()
    {
        PersonI = new Person();
        this.AddCommand = new DelegateCommand(this.Add);

        // get OccupationList and EmployeeList 
    }
    ......
}

 public class DBViewModel : PersonViewModel
{


    public PersonViewModel PersonVM { get; set; }

    public PersonViewModel()
    {
        PersonVM = new PersonViewModel();

    }
    ....
} 
<DataTemplate DataType='{x:Type viewModel:DBViewModel}'>
    <StackPanel>
          <TextBox Text="{Binding PersonI.Name}" />
    <ComboBox Name="cboccupation" ItemsSource="{Binding OccupationList}"
    DisplayMemberPath="Name"
    SelectedItem="{Binding SelectedItem}" SelectedValuePath="Id"/>

    <Button Content="Add" Command="{Binding AddCommand}" />

   <DataGrid ItemsSource="{Binding EmployeeList}" CanUserAddRows="True">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Occupation">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding OccupationList}" 
                                  DisplayMemberPath="Name" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

    </StackPanel>
    </DataTemplate> 

【问题讨论】:

  • PersonI 在 PersonViewModel 中,因此您需要这样做 。 ComboBox 也要做同样的事情

标签: wpf mvvm viewmodel


【解决方案1】:

如果您想要一种更简单的方法,我认为您可以使用混合设置数据存储字段并将两个控件绑定到该字段。

【讨论】:

    【解决方案2】:

    您的绑定正在尝试绑定到DBViewModel 上的属性PersonIOccupationList,但是这些属性不存在。

    您需要将它们指向 PersonVM.PersonIPersonVM.OccupationList

    <TextBox Text="{Binding PersonVM.PersonI.Name}" />
    <ComboBox ItemsSource="{Binding PersonVM.OccupationList}" ... />
    

    对于 DataGrid 中的 ComboBox 绑定,这可能不起作用,因为 Grid 中每一行的 DataContext 是一个 Person 对象(由 DataGrid.ItemsSource 指定),我认为 Person 没有一个名为 OccupationList 的属性。

    您需要更改绑定的 Source 以使用具有 OccupationList 属性的对象。

    例如,如果您的 DataGrid 被命名为 MyDataGrid,则该 ComboBox 的以下绑定将起作用

    <ComboBox ItemsSource="{Binding 
                  ElementName=MyDataGrid, 
                  Path=DataContext.PersonVM.OccupationList}" ... />
    

    或者,您可以使用 RelativeSource 绑定让它查找父 DataGrid 对象,而无需指定名称

    <ComboBox ItemsSource="{Binding 
                  RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, 
                  Path=DataContext.PersonVM.OccupationList}" ... />
    

    附带说明,您似乎对绑定和DataContext 有点困惑。我喜欢写关于初学者 WPF 主题的博客,建议阅读 What is this "DataContext" you speak of?。我发现它帮助了这个站点上的许多 WPF 初学者理解基本的绑定概念。 :)

    【讨论】:

    • 谢谢。我设法从文本框中获取值。但是数据网格中的组合框仍然存在绑定问题。
    • 您将 DataGrid 绑定到 Person 对象列表,因此每行后面的 DataContextPerson,而 Person 没有名为 OccupationList 的属性。您需要更改绑定的 Source 以指向正确的对象。如果你给你的DataGrid一个名字,你可以在你的绑定中使用ElementName来指定绑定的数据源,比如&lt;ComboBox ItemsSource="{Binding ElementName=MyDataGrid, Path=DataContext.PersonVM.OccupationList}" ... /&gt;。或者,您可以使用RelativeSource 绑定来查找父DataGrid。我会更新我的答案。
    猜你喜欢
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 2012-07-24
    相关资源
    最近更新 更多