Change/Add control's objects from a static member - wpf

I've created a control with DependencyProperty. I need to change control's appearance when this property changes (even in design time). So, if properties is easy enough I can make bindings to them in XAML.
But in my case I need to change and add another objects/controls to it. I know about PropertyChangedCallback method, but it's a static method so I can't access to control's layout.
Any ideas?

Actually you can get access to your control within PropertyChangedCallback. Just cast sender to your control an do whatever you want(adding/deleting and so on).

Related

Is it possible to use a control's property value in visual state definitions of its template?

When I wrote a custom control in WPF, I defined some visual states in the template, and I wanted to use some of the control's property value in the animation declararion. I tried binding and template binding, but it didn't work.
I googled some articles, because visual state manager has it's own logical tree, the binding to template parent won't work.
I wonder is there any way to archive my goal. If it is not, the visual state would be much less useful.
You cannot use Binding because of Freezable issues.
So, define a StaticResource and change its value in PropertyChangedCallback of your DependencyProperty.
Define a do-nothing VisualState without any Storyboard.
Now, in PropertyChangedCallback in step 2, refresh VisualState by first going to (3.), and then to needed one. This will refresh immediately.

Changing exisiting standard property to DependencyProperty

Is that possible to change for example MediaElement.Position property to DependencyProperty by inheriting MediaElement class and then creating DependencyProperty in a new class from inherited class?
How to do it? AFAIK DependencyProperty stops normal behaviour of default accessor, how to reconnect things to not break up after change?
I want to update Slider.Value through Binding thus I need MediaElement.Position as DependencyProperty. I know I can do it with DispatcherTimer but I think it's not professional solution.
You can create either a custom control (a XAML-less control that inherits from MediaElement) or a UserControl with a MediaElement somewhere in its visual tree.
In either case, create your DependencyProperty, and use the PropertyChangedCallBack (in FrameworkPropertyMetadata) to respond to changes made, and use events to listen the other way.
In either case, you are creating a new control to use instead of the MediaElement. The advantage of a User Control is that you only expose the properties needed. The disadvantage being naturally that you have to expose them all. The custom control will also expose the Position property even though you don't want to use it.

Advantage of Binding?

I am not sure that I fully understand the advantage of binding. For example, if I want to bind a string value to a TextBlock I need to do the following:
Create a class that extends INotifyPropertyChanged
Add a string to that class (say: MyString)
Extend the set method for MyString so that it calls another method (say: OnPropertyChanged)
Create the OnPropertyChanged method to call the PropertyChangedEventHandler event
Then I need to create a new instance of the class, set my TextBlock.DataContext to point to that class, and finally add the XAML bit for the binding.
Can someone explain the advantage of this over simply setting:
TextBlock.Text = MyString;
Thanks!
Any changes to MyString won't be automatically reflected in your UI.
Your code behind will be littered with "when this event occurs, update these pieces of data", so you'll essentially be writing your own messy data binding logic for each and every view.
The advantage is that you can both change and display the value in multiple places, without having to update some method to add another TextBlock assignment each time the value changes. Any new display control just binds itself to the property, the rest is automatic.
Now if you really just set the value in one place and show it in one control, then you're right, there's not much point.
The gain of using Data Binding isn't particularly noticeable for a TextBlock binding to a static string.
However if the value of MyString changes during application runtime it becomes much more useful - especially in a case where the object that owns that property is unaware of the TextBlock. This separation between UI and the underlying data layer can be created using a design pattern such as MVVM.
Data Binding is also useful for more complex properties such as Items in a ListBox control. Just bind the ListBox.Items to a property that is of type ObservableCollection and the UI will automatically update whenever the content of that collection changes.

How to create a dependency property in Silverlight using MVVM?

let say there is a textbox and i want to control the visibility of this control using MVVM, is there a sample on how to do this? First create a dependency property then get it hooked up in the ViewModel. Thanks.
Typically, you wouldn't need to use a dependency property in this case. Dependency properties really only need to be implemented for things like controls themselves, not for determining behavior. Behavior, such as the visibility of an element, can be handled directly via data binding.
Your ViewModel would just have some property, and you'd bind the TextBox.Visibility property directly to the ViewModel property.
The one "sticky point" is that you often will want to have some type of IValueConverter that will convert from your property type to a Visibility enum.

How do I track all instances of a DataTemplate, or a control within a DataTemplate?

I'm using a DataTemplate to apply a View to a ViewModel. I have a case where when a certain thing happens in one instance of the View (DataTemplate), I need to take an action in all other instances.
I'm already accomplishing this by implementing the Initialized event on one of the controls in the DataTemplate, and using that event to add a reference to the control to a list in the codebehind. This is working, since there's only one codebehind for the DataTemplate (in a resource dictionary), they can all access the same list.
I'm a bit worried though, since instances of the DataTemplate get created and destroyed. Am I not keeping extra references around to old instances of the DataTemplate that are no longer necessary? Is there some way I can clean them up? Is there a corresponding event... the opposite of Initialized... when a control or DataTemplate is gone?
Would it be feasible for you to model this interaction directly in the ViewModel? Perhaps with a property or event on the item(s) being data-bound? That way your view only has to decide the behavior/presentation of the event.

Resources