gpt4 book ai didi

c# - 将 WPF DataGridComboBoxColumn 与 MVVM (WAF) 绑定(bind)

转载 作者:太空宇宙 更新时间:2023-11-03 13:52:34 25 4
gpt4 key购买 nike

帮我解决绑定(bind)问题。该项目在 WPF + WAF 中+ 首先是 ef 代码。我想将 DataGridComboBoxColumn 值绑定(bind)到模型属性,但有些东西不起作用。型号:

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>

和 View 模型:

[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; }

...

}

在尝试向网格添加新行时,我可以从组合框中选择一个值并添加“金额”和“价格”的值。在 Controller 方面,在处理 EditListCommand 时存在 _editDocumentViewModel.SelectedDocumentMove.Amount_editDocumentViewModel.SelectedDocumentMove.Price 的值,但 _editDocumentViewModel.SelectedDocumentMove.RawMaterialId 的值_editDocumentViewModel.SelectedDocumentMove.RawMaterial 为空。我认为我的 ComboBoxColumn 绑定(bind)中的某些内容是错误的,或者可能是其他内容?

我看过几个类似的问题1 , 2 , 但 无法找到修复方法。

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

最佳答案

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

关于c# - 将 WPF DataGridComboBoxColumn 与 MVVM (WAF) 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13283215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com