Datagrid - 双向绑定需要Path或XPath

时间:2016-02-26 14:48:53

标签: wpf mvvm

我面临的问题是:

1.我有一个数据网格,其中有多个DataGridTemplateColumn,在这里我显示了一个文本块,它应该显示从组合框中选择的值。
2.我已应用交互触发器(PreviewKeyDown,DropDownClosed)和事件触发器 3.我使用MVVM绑定了控件属性和事件的值 4.在数据网格中添加/删除/保存记录时,我收到以下错误。(Datagrid - 双向绑定需要Path或XPath。)

----------------取景功能--------------------------

<DataGrid VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" RowStyle="{StaticResource ItemContStyle}"  AlternationCount="2" Name="lstExpenses"  AutoGenerateColumns="False"  Width="{Binding ActualWidth,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Canvas},Mode=OneWay}"  Height="{Binding ActualHeight,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Canvas}, Mode=OneWay}" ItemsSource="{Binding List,Mode=TwoWay}" SelectionUnit="Cell" DataGridCell.Selected="lstExpenses_Selected" SelectionMode="Extended"  IsReadOnly="{Binding Path=ReadOnlyList,Mode=OneWay}">
        <DataGrid.Columns>
                    <DataGridTemplateColumn Header="Expense Type">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Path=TypeID,Converter={StaticResource NameConverter},ConverterParameter=ExpenseType, Mode=TwoWay}" Style="{StaticResource CenterAlignment}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                        <DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding Path=DataContext.Type, RelativeSource={RelativeSource AncestorType=DataGrid, Mode=FindAncestor},Mode=OneTime}" DisplayMemberPath="Type" SelectedValuePath="TypeID" MaxDropDownHeight="150"  Text="{Binding Path=DataContext.TypeText,RelativeSource={RelativeSource AncestorType=DataGrid,Mode=FindAncestor},Mode=OneWayToSource}" IsEditable="True" IsTextSearchEnabled="False" SelectedValue="{Binding RelativeSource={RelativeSource Self}, Path=DataContext.TypeID,Mode=TwoWay}">
                                <i:Interaction.Triggers>
                                   <i:EventTrigger EventName="PreviewKeyDown">
                                            <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}, Path=DataContext.PreviewKeyDownCommand}"                                   CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow, Mode=FindAncestor}}"/>
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                    <ComboBox.Triggers>
                                        <EventTrigger RoutedEvent="TextBoxBase.TextChanged">
                                            <BeginStoryboard Name="DropDownOpen">
                                                <Storyboard>
                                                    <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsDropDownOpen">
                                                        <DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:0" />
                                                    </BooleanAnimationUsingKeyFrames>
                                                </Storyboard>
                                            </BeginStoryboard>
                                        </EventTrigger>
                                   </ComboBox.Triggers>
                                </ComboBox>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellEditingTemplate>

             </DataGridTemplateColumn>
    </DataGrid.Columns>
    </DataGrid>

-----------------------------------------视图模型----- -------------------------------------------------- ------------------

Private Sub Type_PreviewKeyDown(param As Object)


        Dim dtrv As DataRowView
        If Keyboard.IsKeyDown(Key.Enter) Or Keyboard.IsKeyDown(Key.Tab) Then
            If IsNothing(param) OrElse Not TryCast(TryCast(param, System.Windows.Controls.DataGridRow).DataContext, DataRowView).IsEdit Then Exit Sub
            dtrv = TryCast(TryCast(param, System.Windows.Controls.DataGridRow).DataContext, DataRowView)
            Dim dtExpense As DataTable
            Dim dv As DataView
            dv = New DataView(Type)
            If Not (String.IsNullOrEmpty(_strType) Or _strType = "") Then
                dv.RowFilter = "Type = '" & _strType.Trim & "'"
            End If
            If dv.Count <= 0 Then
                MsgBox("" & _strType.Trim & " does not exist in the list.")
                TypeText = ""
                Exit Sub
                blnEventActivateListView = True
                Return
            End If
            CalculateExpenseAmounts(dtrv)
            List.AcceptChanges()
            ShowStatus(False)
        End If

End Sub

Public Property List As DataTable
    Get
        If Not IsNothing(_dtList) Then
            Return _dtList
        else
        MsgBox("Hello")
        End If
    End Get
    Set(value As DataTable)
        _dtList = value
        OnPropertyChanged("List")
    End Set
End Property


Public ReadOnly Property Type As DataTable
    Get
        If IsNothing(_dtType) Then _dtType = _objModel.getType()
        Return _dtType
    End Get
End Property


Public Property TypeText As String
    Get
        Throw New NotImplementedException
    End Get
    Set(value As String)
        _strType = value
        If Not (Keyboard.IsKeyDown(Key.Up) Or Keyboard.IsKeyDown(Key.Down)) Then
            Dim strFilteredText As String
            Dim dv As DataView
            dv = Type.DefaultView
            strFilteredText = "Type like '" + value.ToLower + "%'"
            dv.RowFilter = strFilteredText
            Type.AcceptChanges()
        End If
    End Set
End Property

0 个答案:

没有答案