2015-09-08 108 views
2

我试图做的是将ItemSource绑定到我的模型中的表中的所有值,并将SelectedValue绑定到第一个链接到的表的外键。以下是我所尝试过的。将组合框绑定到模型/视图模型

这里是我的两个表: The attribute the client_typeID is linked to is called 'ClientType'. client_type1 is what i want the View to display

注: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是entitiy表的财产。

这里是我的组合框XAML:

<ComboBox Grid.Column="3" Grid.Row="0" MaxHeight="26" Margin="2" ItemsSource="{Binding Client_type_list}" Text="{Binding Record.Client_Type.client_type1}" /> 

所有这一切都为我提供了具有正确的ItemSource和选择正确的值ComboBox。唯一的问题是,当我更改SelectedItem时,Update方法运行时,它更新Client_Type表的描述,而不是实体表的外键。

此外,我试图按照this的例子,但它并没有帮助我。

任何帮助,我将不胜感激。

+0

请同时看看这个回应:http://stackoverflow.com/a/4902454/4424024它解释了在ItemControls上使用绑定(如ComboBox)的SelectedItem,SelectedValue和SelectedValuePath之间的差异。 – Martin

回答

2

实际上,组合框没有ID信息,您还必须绑定VM中的SelectedValue或SelectedItem属性。您应该在viewmodel中创建Client_Type列表,然后将其与视图绑定。我做了一个小POC希望这会给你足够的信息。您的代码看起来像

查看

<ComboBox Grid.Column="3" Grid.Row="0" MaxHeight="26" Margin="2" ItemsSource="{Binding Client_type_list}" 
        ItemTemplate="{StaticResource Client_typeDataTemplate}" 
        SelectedValue="{Binding Record.Client_Type}"> 
<ComboBox.Resources> 
<DataTemplate x:Key="Client_typeDataTemplate"> 
<TextBlock Text="{Binding Client_type1} "/> 
    </DataTemplate> 
    </ComboBox.Resources> 
    </ComboBox> 
     <Button Click="Button_Click" Height="20"/> 

视图模型(我已合并背后的代码的代码视图模型假设你知道这一点)

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     this.DataContext = this; 
     Record = new Entity(); 
    } 

    public List<Client_Type> Client_type_list 
    { 
     get { return GetClientTypes(); } 
    } 

    private Entity record; 
    public Entity Record 
    { 
     get { return record; } 
     set 
     { 
      record = value; 
      OnPropertyChanged("Record"); 
     } 
    } 


    protected void OnPropertyChanged(string propertyName) 
    { 
     var handler = this.PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 


    public List<Client_Type> GetClientTypes() 
    { 
     List<Client_Type> ClientType = new List<Client_Type>(); 
     { 
      ClientType.Add(new Client_Type() { Client_type1 = "a", Client_typeId = "1" }); 
      ClientType.Add(new Client_Type() { Client_type1 = "b", Client_typeId = "2" }); 
     } 
     return ClientType; 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     MessageBox.Show(Record.Client_Type.Client_typeId); 
    } 

} 

现在,当选择改变实录也因为绑定而发生变化。希望这些信息有帮助。

+0

谢谢你的回应,请你进一步解释ItemTemplate =“{StaticResource Client_typeDataTemplate}”。谢谢! –

+0

这是一个数据模板。请看看更新后的答案。 –