views:

493

answers:

1

I am having a textbox and a combobox as a template column. Below is XAML

<wpfkit:DataGrid Margin="3" Style="{DynamicResource SimpleDataGrid}" 
 FontWeight="Normal" MaxHeight="100" CanUserAddRows="True" 
 ItemsSource="{Binding Source={StaticResource odpExistingGHSCodesColl}}" 
 AutoGenerateColumns="False" Name="dgGHS" VerticalScrollBarVisibility="Visible"
 <wpfkit:DataGrid.Columns>
       <wpfkit:DataGridTemplateColumn IsReadOnly="True">
         <wpfkit:DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
             <Image Style="{DynamicResource SimpleImageDelete}"/>
           </DataTemplate>
          </wpfkit:DataGridTemplateColumn.CellTemplate>
        </wpfkit:DataGridTemplateColumn>
        <wpfkit:DataGridTemplateColumn IsReadOnly="True">
          <wpfkit:DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
             <ComboBox  x:Name="cbTGHSCodes" 
      ItemsSource="{Binding Source={StaticResource   odpGHSCodesColl}}" 
      DisplayMemberPath="fldCode" SelectedItem="{Binding Path=fldGHSCodeList}"
      SelectedValue="fldCode" SelectedValuePath="fldDescription"> 
            </ComboBox>
           </DataTemplate>
         </wpfkit:DataGridTemplateColumn.CellTemplate> </wpfkit:DataGridTemplateColumn> <wpfkit:DataGridTemplateColumn IsReadOnly="True"> <wpfkit:DataGridTemplateColumn.CellTemplate>
       <DataTemplate> 
           <TextBox x:Name="tbTGHSCodeDescription" Text="{Binding Path=fldDescription, ElementName=cbTGHSCodes}"> </TextBox></DataTemplate>
   </wpfkit:DataGridTemplateColumn.CellTemplate>
  </wpfkit:DataGridTemplateColumn>
 </wpfkit:DataGrid.Columns>
</wpfkit:DataGrid>

I am having a observable collection (odpGHSCodesColl) in which i am inserting Codes and its Respective description. code is stored is fldCodes property while fldDescription has description. So what i want to achieve is that say if Code P1 has Desc ABC , P2 has DFG, P4 has UHY , then if select P1 from combobox then the corresponding textbox in next column will get filled with ABC , if P2 then DFG and so on. I hope you can understand. I am unable to find any events attached. If possible give me some example in XAML itself so that i need to write lesser code.

+1  A: 

you will not be able to use the name binding method you have outlined above the cell template binding cant resolve properly. In your output window you should see a list of errors related to the binding. even if you could do this binding you would have multiple ComboBoxes with the same x:Name="cbTGHSCodes" (one for each cell in the column) (which one would you bind to even if you could do this in a data template like you are attempting?)

What you need is to set the code property on your underlying object using the combo box binding. When you set the code your underlying object should set the description and then your underlying object should call INotifyPropertyChanged, this in turn will update the UI.

Basically your datagrid will have two columns. Each column will have a Cell template that binds to a property on the item displayed in the row. When the code changes the item in the row will find the new description and set its description property then NotifyPropertyChanged which will alert the UI.

Aran Mulholland
Hi Aran,Could u please, elaborate your example with some relevant code. It would be a great help for me
Amit Ranjan
Hi Aran,With a small hit and try, i am almost close to the solution, but not getting the proper out. That say in the first row of grid if I select a Code then in the collection its get update but in the next column having textbox, nothing is displayed. Similarly when i added next one, then the description of the first row[0] is displayed in row[2] of col[2] and row[0] and row[1] remains blank. What will be possible solution of this.
Amit Ranjan
Here is the VB code:Private Sub cbTGHSCodes_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)If dgGHS.SelectedIndex >= 0 Then Dim cbTGHSCodes As ComboBox = DirectCast(sender, ComboBox) pvExistingGHSCodesColl(dgGHS.SelectedIndex).fldCode = pvGHSCodesList(cbTGHSCodes.SelectedIndex).fldCodepvExistingGHSCodesColl(dgGHS.SelectedIndex).fldDescription = pvGHSCodesList(cbTGHSCodes.SelectedIndex).fldDescription pvExistingGHSCodesColl.Add(New ExistingGHSCodes)End IfEnd Sub
Amit Ranjan
and xaml is <wpfkit:DataGridTemplateColumn IsReadOnly="True" Width="375" Header="Description"> <wpfkit:DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBox x:Name="tbTGHSCodeDescription" Text="{Binding Path=fldDescription, Mode=OneWay, Source={StaticResource odpExistingGHSCodesColl}}" ClipToBounds="False" IsReadOnly="True"></TextBox> </DataTemplate> </wpfkit:DataGridTemplateColumn.CellTemplate> </wpfkit:DataGridTemplateColumn>
Amit Ranjan
have a look here http://sites.google.com/site/wpfprojects/ and download the DataGridtest.zip, it has a simple project that uses two columns one combo one text. im not using the template columns im using the combo column and the text column. i dont think grids should always be in edit mode. anyway it should give you some ideas...
Aran Mulholland