开发者

Setting WPf Datagrid Combobox value to its respective textbox

开发者 https://www.devze.com 2022-12-15 23:52 出处:网络
I am having a textbox and a combobox as a template column. Below is XAML <wpfkit:DataGrid Margin=\"3\" Style=\"{DynamicResource SimpleDataGrid}\"

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号