双向绑定需要datagrid中的Path或XPath错误

时间:2014-09-11 18:12:29

标签: c# wpf xaml xpath datagrid

我有一个带有组合框列的WPF Datagrid。我允许用户向网格添加行。当用户离开行时,我收到此错误。双向绑定需要datagrid中的Path或XPath错误

下面是我的xaml数据网格代码:

<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>

我一直在看多个类似的帖子,但我无法找到解决问题的方法。

1 个答案:

答案 0 :(得分:1)

您可以尝试使用DataGridComboBoxColumn而不是DataGridTemplateColumn。

如果您还有问题,请要求您更新XAML绑定的ViewModel代码。