I am still beginner in wpf - mvvm. I have a datagrid binded with a collection.
I need to populate the first column by the content of my textbox. So each time I add a new row, the first column should already have the content of my textbox.
How can I do that?
View:
<Grid DataContext="{Binding Source={StaticResource invoice}}">
<StackPanel Orientation="Horizontal">
<TextBox Width="71"
Name="InvoiveNumber"
Text="{Binding ??, Mode=OneWay}">
<!-- My textbox -->
</TextBox>
<DatePicker></DatePicker>
<Label Content="Shop" />
<ComboBox Margin="5"
ItemsSource="{Binding Collection, Source={StaticResource shop}}"
DisplayMemberPath="shop1"
Width="53" />
<Label Content="Supplier" />
<ComboBox Margin="5"
ItemsSource="{Binding Collection, Source={StaticResource supplier}}"
DisplayMemberPath="supplier"
SelectedItem="{Binding Selected, Source={StaticResource supplier}, Mode=TwoWay}"
Width="46" />
</StackPanel>
<DataGrid x:Name="dataGridInvoice"
Margin="5"
Grid.Row="1"
ItemsSource="{Binding Collection}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<!-- My column -->
<DataGridTextColumn x:Name="dataGridTextColumn"
Header="Supplier Invoice Nb"
Binding="{Binding suppInvNumber, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Width="*" />
<DataGridComboBoxColumn Header="Ref Supplier"
ItemsSource="{Binding Products, Source={StaticResource supplier}, Mode=TwoWay}"
DisplayMemberPath="refsup"
SelectedValueBinding="{Binding refSupp}"
SelectedValuePath="refsup"
Width="*" />
<DataGridTextColumn Header="Quantity"
Binding="{Binding quantity, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Width="*" />
<DataGridTextColumn Header="Prix/MOQ"
Binding="{Binding unitPrice, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Width="*" />
<DataGridTextColumn Header="Total Price"
Binding="{Binding totalPrice, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Width="*" />
</DataGrid.Columns>
</DataGrid>
<Button x:Name="BtnAdd"
Content="Save"
Command="{Binding SaveCommand}"
Margin="94,0" />
</StackPanel>
</Grid>
ViewModel:
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
}