开发者

How do I handle binding to an entity that doesn't exist?

开发者 https://www.devze.com 2023-04-08 03:45 出处:网络
I\'m trying to understand how binding works between the view model and page controls when on a \'New Item\' page. For example:

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号