2016-06-27 93 views
0

我看過幾個類似的問題,所以請不要快速解僱它。這種情況在這裏看起來不同,我不明白爲什麼它會出錯。我DataGrid有一些綁定按鍵和鼠標點擊:BindingExpression路徑錯誤:''在'當前集合項目'找不到''屬性... BindingExpression:Path = /;

<DataGrid x:Name="gridStudents" ItemsSource="{Binding Source={StaticResource cvsStudentList}}" 
    Margin="2" 
    Height="250" 
    SelectedItem="{Binding SelectedStudentItem, UpdateSourceTrigger=PropertyChanged}" 
    AutoGenerateColumns="False" IsReadOnly="True" IsSynchronizedWithCurrentItem="True" SelectionChanged="gridStudents_SelectionChanged"> 
    <DataGrid.InputBindings> 
     <MouseBinding 
      MouseAction="LeftDoubleClick" 
      Command="{Binding EditStudentButtonClickCommand}" 
      CommandParameter="{Binding /}" /> 
     <KeyBinding Key="Delete" Command="{Binding DeleteStudentButtonClickCommand}" /> 
    </DataGrid.InputBindings> 

我找到了方法論感謝StackOverflow的和現有用戶的貢獻。非常感激。

此問題與MouseBinding CommandParameter有關。程序運行正常,沒有警告。我可以雙擊DataGrid中的任意一行,它的行爲如同設計。

但是,如果我在Visual Studio 2015年檢查輸出窗戶,我可以看到這句話:

System.Windows.Data Error: 40 : BindingExpression path error: '' property not found on 'current item of collection' ''OCLMEditorModelView' (HashCode=43686667)'. BindingExpression:Path=/; DataItem='OCLMEditorModelView' (HashCode=43686667); target element is 'MouseBinding' (HashCode=49684624); target property is 'CommandParameter' (type 'Object')

爲什麼這樣說呢?我使用/,因爲ItemSource是一個CollectionViewSource對象,我明白/調用當前選擇的項目。但是,直到我實際上雙擊一行,這個命令纔會被觸發。

好奇的是我怎樣才能阻止它出現在我的輸出窗口中。

如果我嘗試更改綁定爲每答案我得到這個異常:

Exception error

更新:

這是當前XAML:

<MouseBinding 
    MouseAction="LeftDoubleClick" 
    Command="{Binding EditStudentButtonClickCommand}" 
    CommandParameter="{Binding /, Source={RelativeSource Self}}" /> 

但現在我注意到這在輸出窗口中:

System.Windows.Data Error: 40 : BindingExpression path error: '' property not found on 'current item of collection' ''RelativeSource' (HashCode=472027)'. BindingExpression:Path=/; DataItem='RelativeSource' (HashCode=472027); target element is 'MouseBinding' (HashCode=36454430); target property is 'CommandParameter' (type 'Object')

它似乎工作(我可以雙擊一行,它做我想要的)。那麼我可以停止這個輸出警告嗎?

更新:

所以這是當前XAML:

<DataGrid x:Name="gridStudents" ItemsSource="{Binding StudentsView}" 
    Margin="2" 
    Height="250" 
    SelectedItem="{Binding SelectedStudentItem, UpdateSourceTrigger=PropertyChanged}" 
    AutoGenerateColumns="False" IsReadOnly="True" IsSynchronizedWithCurrentItem="True" SelectionChanged="gridStudents_SelectionChanged"> 
    <DataGrid.InputBindings> 
     <MouseBinding 
      MouseAction="LeftDoubleClick" 
      Command="{Binding EditStudentButtonClickCommand}" 
      CommandParameter="{Binding /, Source={RelativeSource Self}}" /> 
     <KeyBinding Key="Delete" Command="{Binding DeleteStudentButtonClickCommand}" /> 
    </DataGrid.InputBindings> 
    <DataGrid.CellStyle> 
     <Style TargetType="{x:Type DataGridCell}"> 
      <Setter Property="VerticalContentAlignment" Value="Center"/> 
     </Style> 
    </DataGrid.CellStyle> 

我tryign做什麼並沒有改變 - 當用戶雙擊一個DataGrid行(這是boudn到CVS)它會根據該行調用一個命令。

+0

不要使用'Convert.ChangeType'來寫'(T)parameter'。 –

+0

'RelativeSource' *擴展*只對'RelativeSource' *屬性*有效,而不是'Source'。另外'RelativeSource Self'會引用'MouseBinding'這肯定不是你的集合,你甚至在做什麼...... –

+0

@ H.B。更新了問題(底部)。 –

回答

2

在輸入上下文綁定你的DataContext不應該改變,所以我希望正確的綁定是:

CommandParameter="{Binding Path=/, Source={StaticResource cvsStudentList}}" 

以外,你還可以通過RelativeSource綁定到SelectedItem,應相當。

CommandParameter="{Binding Path=SelectedItem, 
          RelativeSource={RelativeSource AncestorType=DataGrid}}" 
+0

謝謝。如果我嘗試使用靜態資源的第一種方法,則雙擊時會出現異常。 –

+0

「例外」不是很有幫助。問題是綁定本身是否工作以及命令實現期望作爲參數傳遞的是什麼。 –

+0

我不能這樣做。該參數是cvs源代碼中的當前**項目**。我會添加我遇到的異常。我所知道的是,我必須使用/單獨運行。但我得到了這個沉默的例外。如果我按照描述擺弄綁定,我會得到異常。你能否在你的回答中顯示我提到的SelectedItem想法?謝謝。 –

相關問題