2

帮我解决绑定问题。该项目首先在 WPF + WAF + ef 代码中。我想将 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 中选择一个值并为“Amount”和“Price”添加值。在控制器方面,在处理 EditListCommand 时,存在_editDocumentViewModel.SelectedDocumentMove.Amountand的值,但and_editDocumentViewModel.SelectedDocumentMove.Price的值是空的。我认为我的 ComboBoxColumn 绑定中有问题,或者可能是其他问题?_editDocumentViewModel.SelectedDocumentMove.RawMaterialId_editDocumentViewModel.SelectedDocumentMove.RawMaterial

我见过几个类似的问题12,但找不到解决方法。

请帮忙,对不起我的英语)。

4

1 回答 1

1

我添加了参数UpdateSourceTrigger=PropertyChanged并且SelectedValueBinding="{Binding RawMaterialId}"它工作!

于 2012-11-10T16:35:43.697 回答