我正在使用WPF - MVVM應用程序。dataGrid列取決於另一列
我必須在空白處添加產品InvoicedataGrid
。
每個產品都有一個參考號refsup
和一個description
。
當我在InvoicedataGrid
中添加一行時,我選擇ComboboxColumn
中的參考號,並在下一列中顯示說明。
我該怎麼做? 試圖保持MVVM模式
查看
<DataGrid x:Name="dataGridInvoice"
Margin="5"
Grid.Row="1"
ItemsSource="{Binding Collection}"
AutoGenerateColumns="False"
SelectedItem="{Binding Selected, Mode=TwoWay}"
SelectionMode="Extended"
SelectionUnit="FullRow"
AddingNewItem="dataGridInvoice_AddingNewItem">
<DataGrid.Columns>
<DataGridTextColumn Header="SuppNb"
Binding="{Binding suppInvNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Width="*" />
<DataGridTextColumn Header="Supplier"
Binding="{Binding supplier, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Width="*" />
<DataGridComboBoxColumn Header="Ref Supplier"
ItemsSource="{Binding Products, Mode=OneWay, Source={StaticResource supplier}}"
DisplayMemberPath="refsup"
SelectedValueBinding="{Binding refSupp}"
SelectedValuePath="refsup"
Width="*" />
<DataGridTextColumn Header="Description"
Binding="{Binding description, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Width="*" />
</DataGrid.Columns>
</DataGrid>
視圖模型
public class InvoiceViewModel : ViewModelBase
{
public Context ctx = new Context();
public InvoiceViewModel()
{
Get(false);
}
private ObservableCollection<Invoice> collection;
public ObservableCollection<Invoice> Collection
{
get
{
return collection;
}
set
{
collection = value;
OnPropertyChanged("Collection");
}
}
private Invoice _selected;
public Invoice Selected
{
get
{
return _selected;
}
set
{
_selected = value;
OnPropertyChanged("Selected");
}
}
private void Get(bool loadDataFirst)
{
if (loadDataFirst) ctx.Invoices.Load();
Collection = ctx.Invoices.Local;
}
private void Save()
{
ctx.SaveChanges();
}
private void Delete()
{
var id = Selected;
var invoice = (from i in ctx.Invoices
where i.idInvoice == id.idInvoice
select i).SingleOrDefault();
Collection.Remove(invoice);
}
private Invoice _currentItem;
public Invoice CurrentItem
{
get
{
return _currentItem;
}
set
{
_currentItem = value;
OnPropertyChanged("CurrentItem");
}
}
#region "Command"
private ICommand saveCommand;
private ICommand removeCommand;
public ICommand SaveCommand
{
get
{
return saveCommand ?? (saveCommand = new RelayCommand(p => this.Save(), p => this.CanSave()));
}
}
private bool CanSave()
{
return true;
}
public ICommand DeleteCommand
{
get
{
return removeCommand ?? (removeCommand = new RelayCommand(p => this.Delete(), p => this.CanDelete()));
}
}
public bool CanDelete()
{
if (Selected != null)
return true;
else
return false;
}
#endregion
}
型號
public partial class product
{
public int idproduct { get; set; }
public string @ref { get; set; }
public int supplier { get; set; }
public string refsup { get; set; }
public string description { get; set; }
public int MOQ { get; set; }
public int unit { get; set; }
public decimal priceMOQ { get; set; }
public virtual foodSupplier foodSupplier { get; set; }
public virtual unit unit1 { get; set; }
}
public partial class Invoice : ViewModelBase
{
public int idInvoice { get; set; }
public string suppInvNumber { get; set; }
public Nullable<int> supplier { get; set; }
public string refSupp { get; set; }
public string description { get; set; }
public virtual foodSupplier foodSupplier { get; set; }
public virtual shop shop1 { get; set; }
}
非常感謝。我會努力的。 – Cantinou