我仍然是wpf - mvvm的初學者。我有一個數據網格綁定了一個集合。C#wpf,從文本框綁定到dataGridColumn
我需要填充我的文本框內容的第一列。 因此,每次我添加一個新行時,第一列應該已經有我的文本框的內容。
我該怎麼做?
查看:
<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>
視圖模型:所有的
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
}
u能解釋什麼叫「應該在每個dataGridRow的第一列我寫在文本框中什麼」的意思...細說它多一點理解 –
第一列?或您的StackPanel中的文本框?爲什麼你綁定到靜態資源而不是默認的DataContext(ViewModel)在你所有的stackpanel控件中?這真的是你想要做什麼? – cscmh99
嘗試** Text =「{Binding CurrentItem.suppInvNumber}」> **對於TextBox,我猜測add函數會將CurrentItem存儲到數據庫並添加到Collection中。 – daniel