Passing a string property to a command via a button CommandParameter in .NET 6
Hello
So I'm trying to pass a string (that's bound to a text field via a data context
<TextBox [...] Text="{Binding SearchString}"/>
which I made sure already works) using a button's CommandParameter
. The relevant button code is as follows (I've hidden layout arguments)
PerformSearch
is a property of the class MainViewModel
that I have defined as a custom class
This does however not work, pressing the button does call the command and I do see
in the program's output, however after testing parameter
is null. If I pass a string directly instead of using a binding, I do get the string as an output
which produces
What am I doing wrong with my previous binding? Maybe this is the wrong way of handling button presses? I've seen people just use CommandParameter="Binding"
and pass the whole ViewModel, and it does indeed pass the whole viewmodel, but I feel like it defeats the purpose of having a separate class handle the command.14 Replies
The binding failed for some reason, most likely. Maybe that property doesn't exist, or it's a field, or it had the value null when the binding accessed it, etc?
I initialized it like this so I doubt it
But yeah I assumed it was a binding error
The obvious thing is that it has the value
""
there?
Try turning the binding debug level up, see what's happeningYes but if it's set to "" why is it
null
Well, in fairness the output doesn't say whether it's null or
""
I did add an if condition earlier that tested that, and it was null, but I removed it
But true
If I programatically set _searchString to some value, it works the first time, but then it doesn't get the new values. I think it gets a reference to the first string instance, then once it changes it becomes null. I guess I can't bind to a property like this since it gets a new instance
That's expected, as when you set
_searchString
directly then the PropertyChanged
event won't be fired, and the binding won't know that anything's changed
You shouln't get null
there -- the binding just won't updateI only change SearchString through a data binding
<TextBox [...] Text="{Binding SearchString}"/>
Aha
Looks like the
CommandParameter
property doesn't set the default binding mode to OneWay
, so if you have two read/write bindings... And I can't remember what happens in that case
Try CommandParameter="{Binding SearchString, Mode=OneWay}"
?
And only ever set SearchString
in code, not _searchString
Same as before, actually I noticed that whatever I set
_searchString
to first, it just keeps that value for any subsequent call, with or without OneWay
Now however what I don't understand is that it didn't do that before, I must've changed something I thought was irrelevant and now it acts differently ;_;Can you share your actual test code and its outputs?
MainModelView.cs
MainView.xaml
I just press the "Search" button to do my testing after changing the text in the
TextBox
Dunno if it's relevant, but here's the MainWindow's XAML as well
That sort of thing should work -- I've done it plenty of times myself
Yeah that's why I'm surprised that fails
I mean I'm very knew to .NET and I've ever only had school experience, I'm far more experienced in other frameworks like Qt and it's pretty annoying to be stuck on such a simple problem. If I figure out the solution I'll make sure to share it