WPF MVVM Datagrid具有Combobox更新selectedItem上的其他列更改
我试图从现在开始完成此任务。我正在开发一个使用MVVM框架的基于Windows的应用程序,它具有绑定到observablecollection的Datagrid。第一列是绑定到一个类中的项目的组合框。我想要发生的事情是,在第一次加载时,网格应该用从Combobox设置的数据库中拉出的值填充到合适的值(THIS工作完美),但随后在用户更改组合框值时,相应的其他列将被更新(此部分不工作)....请帮助!WPF MVVM Datagrid具有Combobox更新selectedItem上的其他列更改
这里是我的XMAL,InstrumentType产业应该得到填充基于在仪器名称组合框中选择的值
<DataGrid Grid.Row="3" Grid.ColumnSpan="4" AutoGenerateColumns="False" ItemsSource="{Binding obFundPartipants}" SelectionMode="Extended" SelectionUnit="FullRow" >
<DataGrid.Columns>
<DataGridTextColumn Header="Sr.No" Width="40"/>
<DataGridTemplateColumn Header="Instrument Name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<!--<ComboBox
ItemsSource=
"{Binding DataContext.lstInstrumentMasterAll,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window }}}" DisplayMemberPath="InstrumentName"
SelectedValuePath="InstrumentName"
SelectedValue="{Binding Path=InstrumentDetails.InstrumentName}"
SelectedItem="{Binding InstrumentDetails, Mode=TwoWay}" />-->
<ComboBox
ItemsSource=
"{Binding DataContext.obInstrumentMaster,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window }}}" DisplayMemberPath="InstrumentName"
SelectedValuePath="InstrumentName"
SelectedValue="{Binding Path=InstrumentDetails.InstrumentName}"
SelectedItem="{Binding imSelectedInstrument, ValidatesOnDataErrors=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!--<DataGridTextColumn Header="Instrument Name" Width="200" Binding="{Binding Path=InstrumentDetails.InstrumentName}" />-->
<DataGridTextColumn Header="Instrument Type" Width="100" Binding="{Binding Path=InstrumentDetails.InstrumentType}" IsReadOnly="True" />
<DataGridTextColumn Header="Industry" Width="100" Binding="{Binding Path=InstrumentDetails.IndustryName}" IsReadOnly="True"/>
<DataGridTextColumn Header="Pur Date" Width="60" Binding="{Binding Path=TransactionDate}" />
<DataGridTextColumn Header="Quantity" Width="60" Binding="{Binding Path=BalanceQty}" />
<DataGridTextColumn Header="Buy Price" Width="60" Binding="{Binding Path=AveragePrice}" />
<DataGridTextColumn Header="Invested Amount" Width="100" Binding="{Binding Path=AverageAmount}" />
<DataGridTextColumn Header="Cur Price" Width="60" Binding="{Binding Path=InstrumentDetails.CurPrice}" />
<DataGridTextColumn Header="Current Value" Width="100" Binding="{Binding Path=CurrentAmount}"/>
</DataGrid.Columns>
</DataGrid>
View Model
private InstrumentMaster _imSelectedInstrument; public InstrumentMaster imSelectedInstrument { get { return _imSelectedInstrument; } set { _imSelectedInstrument = value; base.NotifyPropertyChanged("imSelectedInstrument"); UpdateInstrumentDetails(); } }
private ObservableCollection<Transaction> _obFundPartipants; public ObservableCollection<Transaction> obFundPartipants { get { return _obFundPartipants; } set { _obFundPartipants = value; base.NotifyPropertyChanged("obFundPartipants"); UpdateInstrumentDetails(); } }
public class Transaction : INotifyPropertyChanged
{
private DateTime _TransactionDate; public DateTime TransactionDate { get { return _TransactionDate; } set { _TransactionDate = value;} }
private String _TransactionType; public String TransactionType { get { return _TransactionType; } set { _TransactionType = value;} }
private int _FundID; public int FundID { get { return _FundID; } set { _FundID = value;} }
private int _PortfolioID; public int PortfolioID { get { return _PortfolioID; } set { _PortfolioID = value; } }
private int _InstrumentID; public int InstrumentID { get { return _InstrumentID; } set { _InstrumentID = value;} }
private String _InstrumentKey; public String InstrumentKey { get { return _InstrumentKey; } set { _InstrumentKey = value;} }
private InstrumentMaster _InstrumentDetails; public InstrumentMaster InstrumentDetails { get { return _InstrumentDetails; } set { _InstrumentDetails = value; } }
private String _InstrumentType; public String InstrumentType { get { return _InstrumentType; } set { _InstrumentType = value; } }
private double _TransactionQty; public double TransactionQty { get { return _TransactionQty; } set { _TransactionQty = value;} }
private double _TransactionPrice; public double TransactionPrice { get { return _TransactionPrice; } set { _TransactionPrice = value;} }
private double _TransactionAmount; public double TransactionAmount { get { return _TransactionPrice * _TransactionQty; } set { _TransactionAmount = _TransactionPrice * _TransactionQty; } }
private String _TransactionNotes; public String TransactionNotes { get { return _TransactionNotes; } set { _TransactionNotes = value;} }
private String _FundName; public String FundName { get { return _FundName; } set { _FundName = value; } }
private String _InstrumentName; public String InstrumentName { get { return _InstrumentName; } set { _InstrumentName = value; } }
private Boolean _TransactionFlag; public Boolean TransactionFlag { get { return _TransactionFlag; } set { _TransactionFlag = value; } }
private DateTime _FundTransactionDate; public DateTime FundTransactionDate { get { return _FundTransactionDate; } set { _FundTransactionDate = value; } }
private double _BalanceQty; public double BalanceQty { get { return _BalanceQty; } set { _BalanceQty = value; } }
private double _AveragePrice; public double AveragePrice { get { return _AveragePrice; } set { _AveragePrice = value; } }
private double _AverageAmount; public double AverageAmount { get { return _AveragePrice * _BalanceQty; } }
private double _CurrentAmount; public double CurrentAmount { get { return _CurrentAmount; } set { _CurrentAmount = value; } }
public void OnPropertyChanged(string p)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(p));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class InstrumentMaster
{
private Boolean _SelectedInstrument; public Boolean SelectedInstrument { get { return _SelectedInstrument; } set { _SelectedInstrument = value; } }
private Boolean _Checked; public Boolean Checked { get { return _Checked; } set { _Checked = value; } }
private int _InstrumentID; public int InstrumentID { get { return _InstrumentID; } set { _InstrumentID = value; } }
private String _InstrumentKey; public String InstrumentKey { get { return _InstrumentKey; } set { _InstrumentKey = value; } }
private String _InstrumentName; public String InstrumentName { get { return _InstrumentName; } set { _InstrumentName = value; } }
private String _InstrumentType; public String InstrumentType { get { return _InstrumentType; } set { _InstrumentType = value; } }
private String _ISINCD; public String ISINCD { get { return _ISINCD; } set { _ISINCD = value; } }
private int _IndustryID; public int IndustryID { get { return _IndustryID; } set { _IndustryID = value; } }
private String _IndustryName; public String IndustryName { get { return _IndustryName; } set { _IndustryName = value; } }
private int _Active; public int Active { get { return _Active; } set { _Active = value; } }
private int _PortfolioID; public int PortfolioID { get { return _PortfolioID; } set { _PortfolioID = value; } }
private double _BalanceQuantity; public double BalanceQuantity { get { return _BalanceQuantity; } set { _BalanceQuantity = value; } }
private int _PersonalFundID; public int PersonalFundID { get { return _PersonalFundID; } set { _PersonalFundID = value; } }
private double _CurPrice; public double CurPrice { get { return _CurPrice; } set { _CurPrice = value; } }
private String _CurPriceType; public String CurPriceType { get { return _CurPriceType; } set { _CurPriceType = value; } }
private DateTime _CurPriceDate; public DateTime CurPriceDate { get { return _CurPriceDate; } set { _CurPriceDate = value; } }
}
回答:
你的属性中的每一个二传手必须调用OnPropertyChanged("<property Name>")
以上是 WPF MVVM Datagrid具有Combobox更新selectedItem上的其他列更改 的全部内容, 来源链接: utcz.com/qa/265909.html