【发布时间】:2015-09-08 09:16:42
【问题描述】:
我正在尝试做的是将ItemSource 绑定到我模型中的表中的所有值,并将SelectedValue 绑定到第一个链接到的表的外键。以下是我尝试过的。
注意:client_typeID 链接到的属性称为“ClientType”。 client_type1 是我希望视图显示的内容。
这是我获取客户端类型列表的 DataAccessService 方法:
public List<string> GetClientTypes()
{
List<string> ClientType = new List<string>();
foreach (var item in context1.Client_Type)
{
ClientType.Add(item.client_type1);
}
return ClientType;
}
这是我的 ViewModel:
public List<string> Client_type_list
{
get { return ServerPxy.GetClientTypes(); }
}
private Entity record;
public Entity Record
{
get { return record; }
set
{
record = value;
RaisePropertyChanged("Record");
}
}
注意:ServerPxy 是我的 DataAccessService 实例,Record 是实体表的属性。
这是我的 ComboBox 的 XAML:
<ComboBox Grid.Column="3" Grid.Row="0" MaxHeight="26" Margin="2" ItemsSource="{Binding Client_type_list}" Text="{Binding Record.Client_Type.client_type1}" />
所有这些都为我提供了ComboBox,该ComboBox 具有正确的ItemSource 和选择的正确值。唯一的问题是,当我更改 SelectedItem 时,它会在运行 Update 方法时更新 Client_Type 表的描述,而不是 Entity 表的外键。
我也尝试遵循this 的示例,但它对我没有帮助。
如果有任何帮助,我将不胜感激。
【问题讨论】:
-
还请看一下这个回复:stackoverflow.com/a/4902454/4424024 它解释了在 ItemControls 上使用绑定时 SelectedItem、SelectedValue 和 SelectedValuePath 之间的区别,例如
ComboBox