The subject really say it all, even if it sounds over-complicated. Please hang on.
I have a DataGrid. There are several columns in the DataGrid.
One of the columns contains a ListView. The listView holds several rows.
Each row contains a TextBlock and a Button.
The TextBlock's Text property contains a name, which I want to pass to my ViewModel.
I have managed to create a small example of this which should be just to "copy-paste-run".
Since it is "a lot" of code I have made a public gist to store it in. Link to source in GIT Gist[^]
* I use Visual Studio 2015 and compiling to NET 4.5.2.
* I also use Fody.PropertyChanged, then I'm not needed to manually type all stuff required for the INotifyPropertyChanged interface.
* Create a new WPF application and call it: WpfTest.
* Save the project.
* Install the Fody.Propertychanged in the NuGet Package Manager.
* Create the FodyWeavers.xml file.
* Paste all code from the Gist.
If the compiler says: "Fody: Could not find a weaver named 'PropertyChanged'. ..."
Remove the <PropertyChanged /> from the FodyWeaver.xml file and try again. That line is not neccessary for this and I don't understand why it sometimes fail when compiling.
This is what I want
If you click any of the buttons in the DataGrid I want the associated name on the same row in the ListView to be bound to the SelectedName property in the ViewModel.
I have no clue how to do the binding of the selected Textblock's Text property to the ViewModel in the View's xaml code.
For example: Press the button to the right of NameA. I then want my ViewModels SelectedName to be "NameA".
Add a click handler / delegate that "stuffs" the "selection"; i.e. "code behind". (It's PLUMBING).
And you don't need all that "property changed stuff". If you know you are refreshing all controls on a container, you can call propertychanged with "no name" (on the container) and refresh all with one statement. You need a really "slow" machine for individual "property changed" events to make a difference.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
I copied your code into a project and ran it successfully. The only thing that's different in my version is that I have a different image (obviously). Check that the image is set to a Build Action of Resource and that Copy To Output Directory is set to Do Not Copy. My successful code:
For those familiar with them, TVPs are a great way to reduce the number of calls to the database and improve performance. I use them to great effect in the MVC web app using the general technique below. In the stored procedure I use a cursor to iterate through the rows and do all the db stuff in one whack.
I copied the same routines to a WPF app I'm doing, however, and it flames out calling the proc. I've since deleted it and gone the multiple calls route so I don't remember the precise error message but in general it was barking about not liking the parameters.
I'm on the same box, same version of VS, etc. and literally copied and pasted my routines. Works in MVC, no joy in WPF.
Have any of you successfully used table value parameters in a WPF app, and if so, is there a trick I'm missing?
ADO.NET is ADO.NET; there's no difference whether you're calling it from ASP.NET, WPF, or a console application.
If it works in your ASP.NET application, but not in your WPF application, then there's a difference in the code which you haven't shown. For example, verify that Command is a SqlCommand instance, and not some other type.
Appreciate the feedback. Yeah, that was my take on it as well - ADO.NET should be ADO.NET. However, after a few decades of dealing with MS technologies, I never take that as gospel.
When I get some time I'm going to try it again as the performance difference is significant, but I eyeballed the code pretty intensely and am as sure as I ever am about such things that it was the same in both environments. I hope to be wrong about that because otherwise I'm out of ideas.
That's a nice approach. The DataTable was from an example I found on TVPs. I was focused on making the db interaction worked and never thought to convert it to something more elegant. Deadlines and all that.
I'm going to take another swing at this today. Hopefully I just fat fingered something when I cloned the MVC code I was using. Appreciate all the help.
Well, apparently the fingers are indeed fat. Tried again this morning and it worked without a fuss.
Still using DataTable approach at this point. MS says SqlDataRecord is resource abusive and recommends not creating a new one but reusing a single instance. That aside, are there any benefits to the enumerable approach over populating a data table?