❔ WPF data binding
Hello, I'm new into wpf developing. I'm trying to bind textbox's data to a property, but it seems to work only for getter, not setter. when I manually set the property and run the app, the textbox is filled by the property. But when, in the app, I change textbox's data, it doesn't update the property. Can somebody help me ?
33 Replies
Can you show us, how you set up the binding? 🙂
I will try to do so
here a textbox's Text is binded to a Scene property
here is the property
here is my view
and that's the viewModel
sorry that's a bit messy
And where are you setting the DataContext?
Furthermore, your second Textbox in
ShotTheme.xaml
binds to shot
which does not exist. You'll have to use Shot
.
And your Shot property will need to notify the UI when something changed as well
Instead of
You can use
Or even drop the entire property
and do just this
The last example creates some source-generated files, so you'll still be able to use Scene
in your code and XAML, even though you never specifically declared it.
This makes it more readable :)
So for Shot
you'll have to choose one of those 3 ways to notify your UI
The same applies for Environment
So I checked the docs. So most of the time, bindings have a default value for UpdateSourceTrigger
of PropertyChanged
. However this doesn't apply to textboxes. So you'll 1.) have to set the binding mode of your bindings to TwoWay
and then, you'll have to set the UpdateSourceTrigger
of your binding to PropertyChanged
which one ? In the view I'm setting the datacontext for the ShotListViewModel, but I don't know if this is the one you talking about
isn't it what the OnpropertyChanged element is for ?
for this last exemple, do I need to generate the ObservableProperty class ?
No, I meant the ShotTheme. Are you setting the datacontext there?
Yes, but only for Shot. All other properties wont notify the UI because they dont have this in your VM
No, it's part of the CommunityToolkit 🙂 you can use it in every class that derives from ObservableObject
ah wait.. i noticed that theres no CommunityToolkit in your usings. ObservableObject is your own implementation then? In that case you'll have to go with the first way ^^
Using the lambda sign ?
I need to set dataContext for a theme ?
Because I'm declaring a resourceDictionary for it in the app.xaml
Nah, just go the same way as with your
Scene
property
I falsely assumed that ObservableObject
comes from a package that is widely used in WPF. That package also has a class called ObservableObject
and it provides the same functionality. They just added a few extras
If you want to bind stuff to the controls in it, then you'll have to have some sort of DataContextHow do I do so ?
What I don't understand, is that the binding works half way
That's because
Furthermore, your second Textbox in ShotTheme.xaml binds to shot which does not exist. You'll have to use Shot.Binding to Properties is case-sensitive
Yeah I know, I was working first on Scene. But scene doesn't work neither
Side-question. Do you really have to bind data into a theme?
Well the listView is composed of items defined in the ShotTheme
But I suppose it's possible to bind into a theme
When the element scene of the object is set in the code, it's working because the textbox is set with the value defined. So the binding is working. But when in the app I modify the property, if I print scene in the output window, the value isn't updated.
No, I meant: Do you specifically have to bind stuff in your theme? 😄
You'd still need a DataContext to bind data. A resource dictionary can't have a datacontext
I need to bind the text of textbox which are in the theme, so idk how I can do without it.
Try to replace the controltemplate with a datatemplate and specify the datatype
yeah it seems to be a problem around the template, but I'm not used to xaml enough to re-template everything, I think I will have to do some research
So tl;dr
If you just replace "ControlTemplate" with "DataTemplate", you can then assign a
DataType
to the template. If you do that, you can then use the properties of this datatype in Bindings of control properties within the templateBut can I do that within a style ?
because my listView has the property ItemContentStyle set to the style ShotTheme
so I need a style, but if I'm right I can't use dataTemplate in a style
I will try using a dataTrigger outside Style
well no I can't do that
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.Any other ideas ? i'm stuck
What about changing the TargetType to
ListView
and the targeted property of the setter to ItemContainerStyle
? Then you can use DataTemplate
I've tried like that
but it's still not working
I will try your version
nope, not working
So maybe the problem is somewhere else
ok so I know where the problem is from. In the shotTheme above, I'm defining a couple of textBox. They all share the same style TextboxTheme. If I remove the style, the binding of Scene is working, the value is updated correctly. What I don't understand is why, with the theme, it's not working.
maybe it's because I'm redefining the Textbox' Style. How can I tell the textBox's Text property of TextboxTheme = textBox's Text property of ShotTheme ?
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.