Rickie Marsh
Rickie Marsh

Reputation: 53

Wpf without event handlers

Edit: I misread the assignment.... No event handlers because he doesn't want us working ahead, we're just to bind a set of data to controls to be displayed.

Small issue with homework in wpf. We're building on a program each work, we're on binding right now. The user enters data into a textbox, and it displays two sets of data into two datagrids. Right now, what is displayed is hard coded instead of it pulling from a database. I can get it to display the data and I can get it to display the correct data based on the textbox input, but we're not allowed to use event handlers. So how can a button click be read without an event handler for it?

I can bind the textbox and the window's title so it changes as the text is changed within the textbox, but I can't get what is in the textbox. It's possible I've overlooked something in the book, but I don't see how this is possible without event handlers for the button at least.

Upvotes: 0

Views: 309

Answers (3)

Ucodia
Ucodia

Reputation: 7710

If you are not allowed to use events and are required to use a button in your interface, then commands are definitely the way to go.

Actually commands are the prefered solution for interaction over events since the release of WPF. As I am mostly developing using MVVM, I can't remember the last time I hooked on a button event.

Also every other way of linking your button to an interaction would be a dirty hack.

Maybe it is a bit of a step forward into your courses, but still it worth reading: http://msdn.microsoft.com/en-gb/magazine/cc785480.aspx

Upvotes: 0

denis morozov
denis morozov

Reputation: 6316

Another way is to bind a button to a Command, that's what truly replaces the event

 <Button Command="{Binding ViewModel.YourCommand}"

 <Button Command="{Binding ViewModel.Cmd}" CommandParameter="{Binding ViewModel.CmdParam}"/>

The in the ViewModel, you do all the logic. Although you could put the command in YourView.xaml.cs, that depends on your MVVM design (meaning if you feel that like that functionality doesn't belong in the ViewModel)

Upvotes: 2

Vlad
Vlad

Reputation: 35594

You can multibind the datagrid to 2 things: your source data and the textbox. In the converter you can analyse the textbox's content and pick the right part of the data to display.

Upvotes: 1

Related Questions