Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am converting a VBA project into C# and WPF.
I'm affraid that I just do not understand BINDING.

NOTE: I am using LINQ to Entities to access data from an SQL Database.

My problem is as follows:
(This is just a small fictitious example. In reality these tables have many fields)
1. I have a table called CLIENT which has 2 fields:
(ClientCD and ClientCountryCD)
2. I also have a table called COUNTRY which has 2 fields:
(CountryCD and CountryName)
3. CLIENT is related to COUNTRY via the ClientCountryCD.
4. In XAML I have a row which contains:
a) TextBox to display ClientCD - followed by
b) ComboBox to display ClientCountryCD from CLIENT table and
select CountryCD from the COUNTRY table.
c) TextBox (disabled) to display CountryName from the COUNTRY table
that reflects the ClientCountryCD both initially and when another
is selected from the ComboBox.

How do I perform the required binding?

What I have below is clearly incorrect.

HTML
<Grid DataContext="{StaticResource tblClientsViewSource}"
              Margin="20,200,0,0"
              HorizontalAlignment="Left"
              Name="grid1"
              VerticalAlignment="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Label Content="Client:"
                   HorizontalAlignment="Left"
                   VerticalAlignment="Center" />
            <TextBox Grid.Column="1"
                     Height="23"
                     HorizontalAlignment="Left"
                     Name="cli_ClientTextBox"
                     Text="{Binding Path=cli_Client, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}"
                     VerticalAlignment="Center" Width="120" />
            <Label Content="Country:"
                   Grid.Column="2"
                   HorizontalAlignment="Left"
                   Margin="3"
                   VerticalAlignment="Center" />
            <ComboBox DisplayMemberPath="cli_Country"
                      Grid.Column="3"
                      Height="23"
                      HorizontalAlignment="Left"
                      ItemsSource="{Binding}"
                      Name="cli_CountryComboBox"
                      VerticalAlignment="Center"
                      Width="120" />
            <Label Content="Name:"
                   Grid.Column="4"
                   HorizontalAlignment="Left"
                   VerticalAlignment="Center" />
            <TextBox DataContext="{StaticResource tblCountriesViewSource}"
                     Grid.Column="5"
                     Height="23"
                     HorizontalAlignment="Left"
                     Name="cnt_NameTextBox"
                     Text="{Binding Path=cnt_Name, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}"
                     VerticalAlignment="Center"
                     Width="120" />
        </Grid>
Posted
Updated 15-Mar-12 0:43am
v3
Comments
Wayne Gaylard 15-Mar-12 4:04am    
You can just copy and paste directly into the input box, and a popup will pop up and ask you whether you want to paste as code, html or plain text, so just select code. If you input code with the keyboard, you can just select the code and you will see a code link above the input box which you can use to select the language and it will surround your code with pre tags.
MyPortMan 15-Mar-12 4:36am    
Thanks Wayne. I have put an example in above.
S@53K^S 15-Mar-12 13:23pm    
Hi,

The Combobox that you are actually binding to binding has to change ItemsSource="Binding Path=CurrentDatacontext.ObservableCollection".Then your binding with the combo box will change.You can only set the binding path to those properties that are in the current DataContext.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900