2

What I am attempting to do is to bind the ItemSource to all values in a table from my model and bind the SelectedValue to the foreign key of the table that the first one is linked to. Below is what I have tried.

Here are my two tables: The attribute the client_typeID is linked to is called 'ClientType'. client_type1 is what i want the View to display

Note: The attribute the client_typeID is linked to is called 'ClientType'. client_type1 is what i want the View to display.

Here is my DataAccessService method for getting the list of client types:

public List<string> GetClientTypes()
{
    List<string> ClientType = new List<string>();
    foreach (var item in context1.Client_Type)
    {
        ClientType.Add(item.client_type1);
    }
    return ClientType;
}

Here is my 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");
    }
}

Note: ServerPxy is my instance of the DataAccessService, Record is the property for the entitiy table.

Here is my XAML for the ComboBox:

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

All of this provides me with the ComboBox that has the correct ItemSource and the Correct value selected. The only issue is that when I change the SelectedItem it updates the Client_Type table's description rather than the foreign key of the Entity table when the Update method is run.

Also I have tried to follow this example however it did not help me.

Any help would me much appreciated.

Community
  • 1
  • 1
Ryan Searle
  • 1,597
  • 1
  • 19
  • 30
  • Please also take a look at this response: http://stackoverflow.com/a/4902454/4424024 It explains the differences between SelectedItem, SelectedValue and SelectedValuePath when using bindings for on ItemControls like the `ComboBox` – Martin Sep 08 '15 at 10:20

1 Answers1

2

Actually combo box does not have ID info also you have to bind the SelectedValue or SelectedItem property in VM. You should create the list of Client_Type in viewmodel and then bind it with the View. I did a small POC hope this will give you enough information. Your code will look like

View

 <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"/>

ViewModel (I have merged the code behind code with viewmodel assuming that you know this)

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);
    }

}

Now when the selection is changing of the Record is also changing because of binding. Hope this information helps.

D J
  • 6,908
  • 13
  • 43
  • 75