开发者

Update database directly from XAML

开发者 https://www.devze.com 2023-01-25 22:52 出处:网络
I have a WPF app that is currently driven completely from XAML.All fields, datagrids everything are bound from XAML using ObjectDataProviders objects.I have a DataAcce开发者_如何学GossLayer (DAL) impo

I have a WPF app that is currently driven completely from XAML. All fields, datagrids everything are bound from XAML using ObjectDataProviders objects. I have a DataAcce开发者_如何学GossLayer (DAL) imported into the project. The calls to retrieve data using the ODPs are being used to display the data are all working.

Is it possibly to perform inserts, updates, deletes using the methods that exist in my DAL directly from the XAML? I am trying to avoid ANY code-behind here at all to keep the UI a separate entity. I cannot find any clear cut examples with insert, update, deletes on this. Any thoughts?


Neither WPF nor Xaml really have any concept of inserts, updates, and deletes. Data binding is all concerned with objects (or occasional XML). Once changes in the UI (e.g., the user typing a value into a TextBox) have been pushed into the bound objects, data binding's job is done. It's up to you what you go on to do with the data.

Not that this is any obstacle to the "no code-behind" goal. The usual approach is to add a layer between your view (Xaml) and your data objects, and to bind the UI to that layer rather than the underlying domain model. (This layer is often called the ViewModel, but you'll also see it go by other names such as the Presenter. And this overall pattern is known as 'separated presentation'.)

The way you enable actions without needing codebehind is through the use of commands. Your intermediate layer doesn't just expose data-oriented properties. It can expose properties that hold commands, and you can bind those commands to buttons, hyperlinks, or menu items. This doesn't require any codebehind - it's all just binding expressions in the Xaml. But when you button is clicked, the relevant handler code in your viewmodel runs. (ICommand is a very simple interface to implement - a button will just call the Execute method when clicked. And you do normally provide your own implementation - the built-in RoutedCommand and RoutedUICommand are inappropriate for this scenario.)

And then you can insert/update/delete/whatever as you see fit.

0

精彩评论

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