[英]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
这是你ItemSource
的ComboBox
。
Since I don't have your exact class definitions please change the code where needed. 由于我没有您确切的类定义,请在需要的地方更改代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.