0

Iam Trying to Bind "Description" column value of dataGrid to command parameter of a button inside same grid row but, Binding not working properly. code:-

   <DataGrid x:Name="devicelist" Background="SkyBlue" AutoGenerateColumns="False"  ItemsSource="{Binding Path=DeviceList}" SelectionChanged="devicelist_SelectionChanged"  >
 <DataGrid.Columns>  
                <!--Description-->
                <DataGridTextColumn Header="Descrition" Width="*"  IsReadOnly="True" Binding="{Binding Path=Description}">
                </DataGridTextColumn>                         
                          
                <!--testcaseSelection button-->
                <DataGridTemplateColumn Header="TestCases" Width="*" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType= {x:Type Window}}, 
                             Path=DataContext.CurrentViewModel.SelectTestCommand}" CommandParameter="{Binding Path=Description}" >TestCases</Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
 </DataGrid.Columns>
</DataGrid>

the command that i have binded is working properly and getting called also.

  • Please clarify: "Binding not working properly" - how exactly? What do you expect to see and what do you get instead? – Legkov Ivan Jan 17 '23 at 06:20
  • i have a list(deviceList) which i have binded to datagrid. this Description coloumn and testcase button are part of same row. and initialy when dataGrid is Loading and the value getting passed as Command Parameter is NULL. Even though description column is populating with data. note:- and if iam clicking several time on the Row then automatically its Getting correct value. So can you tell me what could be the problem. and if the way Iam binding command and command parameter is correct. – abhay singh Jan 17 '23 at 07:13
  • Does this answer your question? [WPF CommandParameter is NULL first time CanExecute is called](https://stackoverflow.com/questions/335849/wpf-commandparameter-is-null-first-time-canexecute-is-called) – Orace Jan 17 '23 at 12:21
  • no this is not working. please suggest some other solution. – abhay singh Jan 27 '23 at 06:55

1 Answers1

-1

As explained here, try to declare the CommandParameter before the Command:

<Button CommandParameter="{Binding Path=Description}"
        Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType= {x:Type Window}}, 
                          Path=DataContext.CurrentViewModel.SelectTestCommand}"
        Content="TestCases" />
Orace
  • 7,822
  • 30
  • 45