I have a WPF Datagrid which has a combobox column. I allow the users to add rows to the grid. when the user moves out of the row I get this error. Two way binding requires Path or XPath error in datagrid
Below is my xaml datagrid code:
<DataGrid Name="grdTests" HorizontalAlignment="Left" Margin="5,5,0,0" Grid.Row="1" VerticalAlignment="Top" Height="Auto" MinHeight="425" Width="1034"
ItemsSource="{Binding Path=TestList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" AlternationCount="2"
AutoGenerateColumns="False" MinRowHeight="35" CanUserAddRows="True" BeginningEdit="grdTest_BeginningEdit" CanUserDeleteRows="False"
AddingNewItem="grdTest_AddingNewItem" RowEditEnding="grdTest_RowEditEnding">
<DataGrid.Columns>
<DataGridTextColumn Width="150">
<DataGridTextColumn.Header>
<TextBlock Width="133" Text="UserName" ToolTip="Enter the User Name"/>
</DataGridTextColumn.Header>
<DataGridTextColumn.Binding>
<Binding Path="UserName" Mode="TwoWay" >
<Binding.ValidationRules>
<localVal:ValidationRules/>
</Binding.ValidationRules>
</Binding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
<DataGridTemplateColumn Width="230">
<DataGridTemplateColumn.Header>
<TextBlock Width="220" Text="Test Name" ToolTip="Test"/>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
SelectedValue="{Binding Path=TestId,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="TestName" SelectedValuePath="TestID" SelectionChanged="ComboBox_SelectionChanged"
ItemsSource="{Binding Path=DataContext.MainList,RelativeSource={RelativeSource AncestorType=local:AddEditTests}}">
<ComboBox.Text>
<Binding Path="TestName" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<localVal:ValidationRules/>
</Binding.ValidationRules>
</Binding>
</ComboBox.Text>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridCheckBoxColumn Header="Is Active" Binding="{Binding Path=IsActive,Mode=TwoWay}">
</DataGrid.Columns>
</DataGrid>
I have been looking at multiple similar posts but I couldnt find a solution to my issue.
You can try using DataGridComboBoxColumn instead of DataGridTemplateColumn .
If you further face problem , request you to kindly update the ViewModel code to which the XAML is binded .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.