I have a project in WPF 4 and VB.net 2010. I want to bind the volume property of a mediaelement to a variable in another module. Mind you, the variable is in the correct data type, so no conversion is necessary. How do I do this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First, make sure that your code behind implements INotifyPropertyChanged.
All that is is a way to notify the UI that value for the Volume has changed and it needs to grab the new value on the binding.
The second thing is you’ll need to somehow access the variable from the other module in your code behind, and then reference that in your code behind using a Property.
Now the trick is, when you set the property, you’ll want to also call the notify property changed event.
I can’t remember if the code behind class automatically sets itself as the DataContext for the User Control, so you may want to drop in a “this.DataContext = this” or VB.NET equivalent in your constructor. Usually the DataContext is pulled in automagically from your ViewModel. Basically, that says to use the specified class (aka code behind) as the source for all data bindings.
Then in XAML it’s a usual databinding.
As a side note, this is really not how one usually goes about setting up a WPF application, so this scenario’s a little odd. If you plan to do much with WPF, you may want to look into Josh Smith’s resources on MVVM – that’s really the intended architecture of a WPF application.
Best of luck!