How do I handle binding to an entity that doesn't exist?
I'm trying to understand how binding works between the view model and page controls when on a 'New Item' page. For example:
TransactionView.xaml.cs
public TransactionsView()
{
InitializeComponent();
this.DataContext = App.ViewModel;
}
If I have a list of Transactions, I would do something like this, where AllTransactions
is of type ObservableCollection
.
<ListBox Margin="12,15,12,0" Height="Auto" x:Name="lb_Transactions"
HorizontalAlignment="Stretch" Grid.Row="2" Grid.ColumnSpan="2"
ItemsSource="{Binding AllTransactions}"
ItemTemplate="{StaticResource TransLis开发者_JAVA技巧tDataTemplate}">
</ListBox>
What happens when I have a 'New Transaction' page, which contains a simple form that contains input controls for the user to input text. When the user clicks save, I would create a new Transaction
object, populate it using the data from the form, and add it using App.ViewModel.SaveTransaction()
.
What do I bind the controls in the UI on the New
form to?
I would probably create a new Transaction
first then open the dialog which only manipulates said transaction (pass in constructor and save reference in a property for binding). If the dialog is confirmed the object can be added to the collection, if it is cancelled the object can just be ignored (and go out of scope if created as local variable).
精彩评论