I wonder if it's possible to serialize an attached property with XamlWriter.
I want to add an attached property to a text element and let the XamlWriter handle the
serialization (with a call to XamlWriter.save(myTextElement)).
Thanks!
Related
I am having a bit of trouble understanding the correct way to do the following:
The data I am binding to exists on the internet as a json file. On a timer tick, I download it and using a JavaScriptSerializer, I deserialize it into a class.
Now, I want to bind to that data but when I deserialize, it creates a new class, so my binding breaks (meaning I have to set the ItemsSource or DataContext again).
Does anyone know a way around this?
Thanks!
What is the control that you are trying to bind your data to? If you can bind an observable collection as your data source, all you need to do is to clear your observable collection before fetching the data, and just add the fetched record to the collection post deserialization.
If you do not use ObservableCollection, you can add public properties to your ViewModel and just refresh those when you get the data back. This will ensure the refresh happens becuase your view is bound to public properties of your view model and not aware of the object returned from the call.
After binding deserialized data you should call PropertyChange event of the property you want rebind. In case the selected value is in the new (deserialized) collection it should be reselected.
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.
I have a dependency property exposed in my control that takes its value from another object that already implements INotifyPropertyChanged.
Is there any way to tell WPF to subscribe directly to this object, or do I have to plumb the change notifications myself?
Cheers
I'm may be unclear on your question, but you could do:
control.SetBinding(YourDependencyProperty, new Binding("YourProperty") { Source = poco });
You can also set Mode to TwoWay to pass values back to the POCO.
e.g.
TextBox has a Text property, but I cannot bind to it, if I am going to bind, I have to bind to the TextProperty dependency property.
e.g.
textbox.Text = new Binding("mypath"); does not work
and I need
textbox.SetBinding(TextBoxBase.TextProperty, "mypath")
BUT, and this is a huge but, I don't know that it is property "textbox.Text" until runtime. I'm trying to set the binding via relection information, so I know I have a framework element, and I know i have some property. it might be text, it might be itemssource, or something else.
so given an arbitrary property that is backed by a dependency property identifier, how can i find the dependency property identifier for that property?
While not required, the strongly encouraged convention is to append the word Property to the CLR property when naming the DependencyProperty field. I would start by looking for that.
The System.ComponentModel.DependencyPropertyDescriptor class may help you out here.
However, my recommendation would be to ask, from a larger perspective, why do you believe you need to choose that binding at runtime? I suspect there's probably a better way.
I have a an object which is set to the DataContext in a Window. I have textboxes in the window which are bound to the properties on the object. There seems to be a delay however before the properties on the object are updated.
<TextBox x:Name="txtPropertyOne" Text="{Binding Path=PropertyOne,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
If I change the values in a few textboxes then quickly try to access the properties to which they map, sometimes there are changes which aren't reflected in the properties of the object. I thought that was what the PropertyChanged UpdateSourceTrigger was supposed to take care of.
If I change the values in a few
textboxes then quickly try to access
the properties to which they map
I can interpret this statement in two ways:
You're trying to access the values on a background thread. In that case, you may be accessing the properties before the UI thread has had a chance to do its thing.
You're using a separate message on the UI thread to check the values. Bindings are updated at a priority lower than Send and Normal. So if your message is priority Send or Normal it will be processed before any pending binding updates.
If this doesn't answer your question, please clarify what you mean by "quickly trying to access the properties".
The basic rule of WPF Databinding is simple:
The target property must be a
dependency property, and you're
already correct, it's bound to Text
property of TextBox.
The source property can be a CLR
object (other than any derived WPF's
DependencyObject), but the object
must employ or implement its own
INotifyPropertyChanged.
Have you you already implemented INotifyPropertyChanged on your object?