我仍然是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