✅ How to get data out of a custom control?
Currently, I'm doing a custom user control for a password box with a label showcasing what to put in there.
Right now, this is the content of my user control:
And this is the .cs file:
But as soon as I use it somewhere to display something, I get this error:
System.Windows.Markup.XamlParseException: A 'Binding' cannot be set on the 'Password' property of type 'PasswordBox'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
How can I fix it? I need to get the password out of the password box from my user control, but since I cannot use a property apparently, I don't know another way to do it.56 Replies
The
Password
property in PasswordBox
is not a DependencyProperty
, so you can't use Binding
Your best solution would be setting up a handler for the PasswordChanged
eventDo you mean something like this?
XML:
Yeah, that should work
It does not :'D And I'm struggling to understand why
I use them like this:
In my VM it's setup like this (I use CommunityToolkit.Mvvm):
Hmmm, the event
PasswordBox_PasswordChanged
gets called right?
Also if you're using CommunityToolkit.Mvvm you can shorten your code a bit like this
```cs
[ObservableProperty]
private string _clientSecret;
[ObservableProperty]
private string _clientID;
```
These attributes on the fields will automatically generate properties
You'll need to set the class to
partial` as wellYes, the event gets called
And ty for the tip with the
ObservableProperty
attribute, i forgot it existedit also changed the property correctly when i typed in "a"
Hmmmm
Have you tried setting the mode to "TwoWay"?
@Furina.♡
OneWayToSource
should also workNow it works!! Ty a lot
It was the mode i needed to provide
:HYPERS:
Ok I was happy too early.
When I type in the box now and press on my save button, it saves it to my settings file.
But when I change the view to sth else and then back to my Settings View, the boxes are empty again. When I change to the view, the view model gets the data from the settings file tho.
Any idea on how to fix that?
if you're using CTK why do u have all the boilerplate in there still?
also how are u setting the datacontext of the settings?
are u using DI or its manually done
I set it in the view via
that doesn't set the context
It doesn't?
nope
alas its empty
Do i need to do it like this then?
well it depends
are u using Dependency injection?
if not then yes u would manually set it either there
or when u create the window
and that means if u dispose of that window u would required logic to load the settings when its created
i.e.: wiring the Load method of the View into the VM so it can load the settings when its created
How exactly do i do that? It's my first time using MVVM + WPF
And I'm not using dependency injection
ok
then u create the Load event in your view
for example:
And have a method in your ViewModel called OnLoaded for example.
so what this would do is, when the view is loaded, it would call the Loaded event, which in turn would call the VM method
which would do the loading of your settings
either deserializing from a file and populating the properties in your VM or something I dont know how you're saving the settings
I have a static class dealing with the handling of the settings stuff in the file
So I basically do this in my view model now:
I get the current ClientID and the ClientSecret from my file. This already works.
My view is a UserControl. I've done it like this now:
And it still doesn't work :'D
sure
When I'm debugging the
LoadSettings
method with a breakpoint, it gets into the if statement instead of the else statement
With the corresponding data for ClientID and ClientSecretok so Loadsettings is working?
Yes, that method is working
inside the vm correct?
Yes
But the view does not get the updated properties
can you post your VM?
remove the LoadSettings from the constructor
and check if its being called still
It's removed and it's still being called
k
is your project on github? Apparent I dont see anything off right now
It is, but it's a private repository currently
well can u make it public or something so I can clone and see whats up
I unfortunately cannot because it's for an academic assignment and it will look very weird when other people are also in there :'D
understood, well I can only think your usercontrol is at fault then something missing or done wrong
because on vm/view side things look ok at first glance
and as u said your self things are connecting properly except with your control
I can post my usercontrol xml here if it might help
would also need to see these 2
<ResourceDictionary Source="/ResourceDictionaries/Buttons.xaml"/>
<ResourceDictionary Source="/ResourceDictionaries/Texts.xaml"/>
and PasswordBoxWithLabel
can u do $paste instead
If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
because the file embed from discord is awful
Sure
and it will be more organized with all files in one paste
BlazeBin - bxrwowsorycb
A tool for sharing your source code with the world!
I dont know how to rename the first file, but the first one (newfile) is the Buttons.xaml
And here is the MainWindowVM https://paste.mod.gg/gsaczheztbgq/0
BlazeBin - gsaczheztbgq
A tool for sharing your source code with the world!
missing TextColorBrush
(In the Application resources)
StylizedPasswordBox
ah nvm its there
Should be in this link in TextBoxes.xaml
yeah I just hadn't open it yet
I will have to leave my sit for a few but I will get back to u in a few
Alright, I'ma take a break from coding in the meantime
so yeah I just recall you cant bind to it because its a SecureString
and u can't just dp into it with a string
so the "Password" is conflicting
what I would suggest is keep the clientid display and hide just the secret
and have the secret be a parameter in the command
or use a helper https://stackoverflow.com/a/889005/450121
which will essentially make the secret available in memory and would pose a risk if u expect people to be trying to reverse eng it
so you would remove the Password dp and property and have something like
And in the settings.xaml in the passwordboxwithlabel
I found a different fix with a helper class which is basically creating a copy of the password and attaching it to the password on load
sure, more complex but works as well, just keep in mind that no matter what it will not be secure
and the password will be in memory
$close
If you have no further questions, please use /close to mark the forum thread as answered
Yea, but I use the password box just to hide it from the user in case they are streaming. That's the only reason I use it