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

时间:2022-05-01 14:14:45

I have an edit usercontrol, that is used to edit Workout details. On that UC there is a combobox that has its datacontext set to another viewmodel of Exercise Equipment. How can i get after opening uc to selected item gets to the property of Workout.

我有一个编辑用户控件,用于编辑锻炼详细信息。在那个UC上有一个组合框,其datacontext设置为另一个视图模型的练习设备。打开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).

Workout属性包括:Name,Description 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时,我将Workout对象传递给它,并将usercontrol的datacontext设置为该对象。

On that UC there is a combobox that has its datacontext set to the ExerciseEquipmentViewModel. I get the itemsource from that viewmodel.

在该UC上有一个组合框,其datacontext设置为ExerciseEquipmentViewModel。我从该viewmodel获取了itemsource。

What i need is to properly bind Workout.IdExerciseEquipment to the Combobox itemsource. When opening UserControl, Combobox.SelectedItem should be the item with Workout.IdExerciseEquipment, but i dont know how to achieve that.

我需要的是将Workout.IdExerciseEquipment正确绑定到Combobox项目源。打开UserControl时,Combobox.SelectedItem应该是具有Workout.IdExerciseEquipment的项目,但我不知道如何实现。

Thanks.

谢谢。

1 个解决方案

#1


0  

There are two ways you can do this:

有两种方法可以做到这一点:

Ancestor Binding like you have done. Just correct the binidng as below:

祖先像你一样绑定。只需更正binidng如下:

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

Element Binding, as below:

元素绑定,如下:

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. I think you should create your property as below in Workout class:

更新:根据您提供的信息,我能够理解Workout对象中存在IdExerciseEquipment属性,我认为这是区分AllExerciseEquipment项目的某种ID。我认为你应该在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为空时,我们通过迭代AllExerciseEquipment(您的ComboBox的ItemSource)将值设置为ID匹配的项目。

Since I don't have your exact class definitions please change the code where needed.

由于我没有您确切的类定义,请在需要时更改代码。

#1


0  

There are two ways you can do this:

有两种方法可以做到这一点:

Ancestor Binding like you have done. Just correct the binidng as below:

祖先像你一样绑定。只需更正binidng如下:

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

Element Binding, as below:

元素绑定,如下:

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. I think you should create your property as below in Workout class:

更新:根据您提供的信息,我能够理解Workout对象中存在IdExerciseEquipment属性,我认为这是区分AllExerciseEquipment项目的某种ID。我认为你应该在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为空时,我们通过迭代AllExerciseEquipment(您的ComboBox的ItemSource)将值设置为ID匹配的项目。

Since I don't have your exact class definitions please change the code where needed.

由于我没有您确切的类定义,请在需要时更改代码。