简体   繁体   English

如何从另一个DataContext绑定ComboBox SelectedItem?

[英]How to bind ComboBox SelectedItem from another DataContext?

I have an edit usercontrol , that is used to edit Workout details. 我有一个edit usercontrol ,用于编辑锻炼详细信息。 On that UC there is a combobox that has its datacontext set to another viewmodel of Exercise Equipment. 在该UC上,有一个combobox ,其数据上下文设置为“运动器材”的另一个视图模型。 How can i get after opening uc to selected item gets to the property of Workout. 将uc打开到选定项目后,如何获得Workout的属性。

Here is the code: 这是代码:

<ComboBox Name="ExeEquComboBox" 
        ItemsSource="{Binding AllExerciseEquipment}" 
        DisplayMemberPath="Name" 
        SelectedValue="{Binding ExerciseEquipment, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Mode=TwoWay}">
    <ComboBox.DataContext>
        <ExerciseEquipmentViewModel/>
    </ComboBox.DataContext>
</ComboBox>

EDIT: posting more details as requested: 编辑:根据要求发布更多详细信息:

Workout properties are: Name, Description IdExerciseEquipment (foreign key to ExerciseEquipment table). 锻炼属性为:名称,说明IdExerciseEquipment(ExerciseEquipment表的外键)。

I have 2 ViewModels: WorkoutViewModel ExerciseEquipmentViewModel 我有2个ViewModel:WorkoutViewModel ExerciseEquipmentViewModel

When i open the UC i pass the Workout object to it and set the datacontext of the usercontrol to that object. 当我打开UC时,我将“锻炼”对象传递给它,并将usercontrol的datacontext设置为该对象。

On that UC there is a combobox that has its datacontext set to the ExerciseEquipmentViewModel. 在该UC上,有一个组合框,其数据上下文设置为ExerciseEquipmentViewModel。 I get the itemsource from that viewmodel. 我从该视图模型获取itemsource。

What i need is to properly bind Workout.IdExerciseEquipment to the Combobox itemsource. 我需要的是将Workout.IdExerciseEquipment正确绑定到Combobox itemsource。 When opening UserControl, Combobox.SelectedItem should be the item with Workout.IdExerciseEquipment, but i dont know how to achieve that. 当打开UserControl时,Combobox.SelectedItem应该是带有Workout.IdExerciseEquipment的项目,但是我不知道该如何实现。

Thanks. 谢谢。

There are two ways you can do this: 有两种方法可以执行此操作:

Ancestor Binding like you have done. 像您一样完成Ancestor Binding Just correct the binidng as below: 只需按如下所示纠正binidng

SelectedValue="{Binding DataContext.ExerciseEquipment, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Mode=TwoWay}"

Element Binding , as below: Element Binding ,如下所示:

ComboBox Name="ExeEquComboBox" 
    ItemsSource="{Binding AllExerciseEquipment}" 
    DisplayMemberPath="Name" 
    SelectedValue="{Binding ElementName=UserControlName,Path=DataContext.ExerciseEquipment}">

Update: from the information you provided I'm able to understand that there is IdExerciseEquipment property in Workout object, i think this is some kind of ID for distinguish AllExerciseEquipment items. 更新:从您提供的信息中,我能够理解Workout对象中有IdExerciseEquipment属性,我认为这是用于区分AllExerciseEquipment项目的某种ID。 I think you should create your property as below in Workout class: 我认为您应该在“ Workout类中如下创建属性:

private string exerciseEquipment;

    public string ExerciseEquipment
    {
        get 
        {
            if (exerciseEquipment == null)
            {
                exerciseEquipment = AllExerciseEquipment.FirstOrDefault(x => x.ID == this.IdExerciseEquipment);
            }
            return exerciseEquipment;
        }
        set 
        { 
            exerciseEquipment = value; 
        }
    }

So by default when ExerciseEquipment is blank, we set a value to the item where ID is matched, by iterating AllExerciseEquipment which is your ItemSource of ComboBox . 因此,在默认情况下,当ExerciseEquipment是空白的,我们设置一个值,其中项目ID匹配时,通过迭代AllExerciseEquipment这是你ItemSourceComboBox

Since I don't have your exact class definitions please change the code where needed. 由于我没有您确切的类定义,请在需要的地方更改代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WPF 将 ComboBox ItemSouce 绑定到 ObservableCollection 并将 SelectedItem 绑定到 DataContext 模型属性 - WPF bind ComboBox ItemSouce to an ObservableCollection and the SelectedItem to a DataContext model property 如何从ListView绑定组合框的SelectedItem / Value / ValuePath - How to bind a Combobox's SelectedItem/Value/ValuePath from within a ListView 将组合框绑定到WPF中的datacontext之外的另一个数据集 - Bind a combobox to another dataset than the datacontext in WPF 如何将不同的属性绑定到ComboBox的SelectedItem和SelectedIValue? - How to bind different properties to SelectedItem and SelectedIValue of ComboBox? 如何将Combobox绑定到带有ListBox依赖的SelectedItem的ObservableCollection? - How to bind a Combobox to an ObservableCollection with SelectedItem dependant on ListBox? 如何在ComboBox中将ItemSource和SelectedItem与字符串绑定? - How to bind the ItemSource and SelectedItem with String in ComboBox? 绑定 SelectedItem 以更正 WPF 中的 DataContext - Bind SelectedItem to correct DataContext in WPF 如何将 Combobox ItemSource 绑定到不在 DataContext 中的属性? - How to bind a Combobox ItemSource to a Property not in the DataContext? ComboBox SelectedItem未设置为x:Bind - ComboBox SelectedItem not set with x:Bind 如何在SelectedIndex更改时获取ComboBox.SelectedItem? - How to get ComboBox.SelectedItem to bind when SelectedIndex is changed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM