2012-11-08 76 views
1

幫我解決綁定問題。該項目首先在WPF + WAF + ef代碼中。我想將DataGridComboBoxColumn值綁定到模型屬性,但某些工作不正常。 型號:使用MVVM(WAF)綁定WPF DataGridComboBoxColumn

public class DocumentMove 
    { 
     [Key] 
     public Guid DocumentMoveId { get; set; } 
     public Guid RawMaterialId { get; set; } 
     public RawMaterial RawMaterial { get; set; } 
     public decimal Amount { get; set; } 
     public decimal Price { get; set; } 
    } 

public class RawMaterial 
    { 
     [Key] 
     public Guid RawMaterialId { get; set; } 
     public RawMaterialGroup Group { get; set; } 
     [MaxLength(20)] 
     public string Code { get; set; } 
     public Colour Colour { get; set; }   
     [MaxLength(100)] 
     public string Name { get; set; } 
     public Measure Measure { get; set; }   
     public List<ArrLocation> ArrLocations { get; set; }   
     public List<RawMove> RawMoves { get; set; } 
     public Delivery Supplier { get; set; } 
     public RawMaterial() 
     {    
     } 
} 

網格:

<DataGrid x:Name="documentMoveTable" AutoGenerateColumns="False" ItemsSource="{Binding DocumentMoves}" 
     SelectedItem="{Binding SelectedDocumentMove}" CanUserDeleteRows="False" IsReadOnly="False" RowEditEnding="documentMoveTable_RowEditEnding"> 
     <DataGrid.InputBindings> 
      <KeyBinding Command="{Binding RemoveCommand}" Key="Del"/> 
     </DataGrid.InputBindings> 

     <DataGrid.Columns> 
      <DataGridComboBoxColumn Header="{x:Static p:Resources.RawMaterial}" 
       SelectedValueBinding="{Binding RawMaterialId}" 
       DisplayMemberPath="Name" SelectedValuePath="RawMaterialId"> 

       <DataGridComboBoxColumn.ElementStyle> 
         <Style TargetType="{x:Type ComboBox}"> 
          <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.RawMaterials}" /> 
          <Setter Property="IsReadOnly" Value="True"/> 
         </Style> 
       </DataGridComboBoxColumn.ElementStyle> 
       <DataGridComboBoxColumn.EditingElementStyle> 
         <Style TargetType="{x:Type ComboBox}"> 
          <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.RawMaterials}" /> 
         </Style> 
       </DataGridComboBoxColumn.EditingElementStyle> 
      </DataGridComboBoxColumn> 

      <DataGridTextColumn Binding="{Binding Amount, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, 
           ValidatesOnExceptions=True, NotifyOnValidationError=True}" 
           Header="{x:Static p:Resources.Amount}" Width="*" ElementStyle="{StaticResource TextCellElementStyle}"/> 

      <DataGridTextColumn Binding="{Binding Price, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, 
           ValidatesOnExceptions=True, NotifyOnValidationError=True}" 
           Header="{x:Static p:Resources.Price}" Width="*" ElementStyle="{StaticResource TextCellElementStyle}"/> 

     </DataGrid.Columns> 
    </DataGrid> 

和視圖模型:

[Export] 
public class EditDocumentViewModel : ViewModel<IEditDocumentView> 
{ 
    private IEnumerable<DocumentMove> _documentMoves;   
    private ICommand _removeCommand;   
    private ICommand _editListCommand; 

    public IEnumerable<DocumentMove> DocumentMoves 
    { 
     get { return _documentMoves; } 
     set 
     { 
      _documentMoves = value; 
      RaisePropertyChanged("DocumentMoves"); 
     } 
    } 

    public DocumentMove SelectedDocumentMove { get; set; } 

...

} 

雖然試圖添加一個新的行格,我可以從ComboBox和廣告中選擇一個值d「金額」和「價格」的值。在處理EditListCommand時控制器一方存在_editDocumentViewModel.SelectedDocumentMove.Amount_editDocumentViewModel.SelectedDocumentMove.Price的值,但_editDocumentViewModel.SelectedDocumentMove.RawMaterialId_editDocumentViewModel.SelectedDocumentMove.RawMaterial的值爲空。我認爲我的ComboBoxColumn綁定中的某些內容是錯誤的,或者可能是其他內容?

我見過幾個類似的問題12,但 無法找到如何解決它。

請幫忙,對不起我的英文)。

回答

0

我增加了參數UpdateSourceTrigger=PropertyChangedSelectedValueBinding="{Binding RawMaterialId}"它工作!