2014-01-12 127 views
1

我試圖從現在開始完成此任務。我正在開發一個使用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; } } 

} 
+0

你應該叫'OnPropertyChanged'每個屬性,它會影響用戶界面的變化,即在'MainWindow'中。嘗試將其稱爲'InstrumentType'屬性。 –

+1

你也想在你的viewmodel中使用一個模型,並且無論出於何種原因都不要複製所有的屬性。 – Jim

+0

@Jim,我知道我有一個冗餘的代碼,我需要清理。我確實有另一個類(InstrumentMaster)在Transaction calss中提到,它只是我第一次試圖讓它工作,然後做清理。 – BigData

回答

0

你的屬性中的每一個二傳手必須調用OnPropertyChanged("<property Name>")

+0

底層類中的值正在更新,只是它們沒有在UI中反映出來。我在Combobox上有UpdateTrigger,但它不起作用 – BigData

+0

XAML中的UpdateTrigger不是你的問題。在您的代碼中,您的屬性的每個設置者都必須調用OnPropertyChange,以便UI知道有關更改。 – gomi42

+0

讓我試試看..... – BigData

相關問題