I'm implementing a custom animation on a custom type (inherits from AnimationTimeline). As I'm using the animation inside VisualStateManager, I can't use the From property - so I didn't even bother to create it. But I do have a To dependency property.
Now, inside GetCurrentValue() I have three parameters, and I counted on using the first one ("defaultOriginValue") as the original value of my animated property. Apparently, it's not the original value but the default value (as specified in FrameworkProperyMetadate for the animated property).
How do I get the actual property value to animate from?
Related
In a drawing application that allows user to add different objects like Rectangle, Ellipse, Text, Image etc, I have a "color box" that lets user see or change the fill of selected object(s). If there are multiple objects selected, I can't obviously show all, so I show the color of first selected object. When user changes the color of "color box", I want to change the fill of all selected objects. (That's also how VS properties window works btw)
How do I bind my "color box"'s Brush property (type Brush) to do this thing?
What I have already tried is this: I bind "color box"'s Brush property to selection object and then use a Converter that examines the selection object and if there is one or more objects in it, it returns the color of first selected object in Convert() function.
Problem however is that I have no way of implementing ConvertBack(), because it doesn't give me access to the selection object, so that I could iterate through the selected objects and assign them newly selected color.
Using MultiBinding is also of no use, as value parameter of ConvertBack() is a single object and doesn't give me access to the selection object. One idea was to supply selection object using ConverterParameter, but ConverterParameter is not bindable.
Another idea (hack?) was to keep a class level variable to point to selection object and assign in Convert() function (by passing selection object in MultiBinding) and then use that variable in ConvertBack(), but I don't know how safe it is and whether Convert() and ConvertBack() will always be called in order.
So how do I achieve this?
OK. This turned out to be easier than I thought. For future readers, I simply added a property to my VM of Brush type (call it SelectionBrush) whose getter would return the Brush of first selected object, and whose setter would iterate through the entire selection and assign the Brush to appropriate objects (by checking their types). No more converters needed!
I have a WPFToolkit DataGrid with at least one column bound (via a proxy object as columns are not part of the visual tree) to a property. I wish to toggle all columns to Visible so that I can perform a calculation based on the DataGridColumnHeader (which is only created when its column is visible for the first time). Having done the calculation I want to reset the column to use the binding that was previously set.
I've attempted to get and store the Binding Expression etc, but with no joy. I have also attempted to use the DependencyObject.SetValue() method to change the property value non-destructively, but this doesn't event correctly change the value, let alone retain the original binding.
Any ideas?
You need to call SetCurrentValue() so that it won't clear the binding. SetValue destroys the old binding.
From MSDN:
This method is used by a component that programmatically sets the value of one of its own properties without disabling an application's declared use of the property. The SetCurrentValue method changes the effective value of the property, but existing triggers, data bindings, and styles will continue to work.
Given you have this
<TextBox Text="{Binding TestProperty}"/>
The SetValue you will overwrite the binding with whatever you provide. If you call SetCurrentValue, however, will ensure that the property takes on the given value, but won't destroy any bindings.
Be aware that you should not use SetCurrentValue in your dependency properties' setter/getter.
SetCurrentValue is more useful in scenarios where you need a property to take on a given value but don't want to overwrite any bindings, triggers, or styles that have been configured against your property.
i have created a user control with three dependency properties
selected value
selected item
selected index
when one of them changes in the call back i am setting the other two e.g if selected value changed i am setting Selected index and selected item which in result triggering the callback of each of other properties where it sets the other two properties and keeps on in loop .how to avoid this situation
That's easy,
in each Property Setter only set the other two when the value you're setting is different from the the existing value (through the call of GetValue()). Add a If statement to check this in each of the Property setters.
You should be more careful with these type of situations, either check if you really need those 3 properties as Dependency Properties or change some of them to normal properties in the ViewModel if you're using an MVVM framework.
If you're using MVVM, many frameworks provide a method to suppress notifications like ReactiveUI (ReactiveObject.SuppressChangeNotifications()) or MVVM Light Tookit.
If I am setting one dependency property equal to another dependency property, will they automatically get updated if one chagnes, or do I still need to bind them. Also if two dp are part of the same object (same type too) and I set them equal to each does the same apply as above, or only this case works and the one above doesn't?
will they automatically get updated if one chagnes, or do I still need to bind them
No. Each is a separate property, and setting one to the other just sets the value. If you want them to be "bound together" then you should create a binding that binds them together.
This is true whether they're in the same class or different classes.
I have a custom control in Silverlight, the change of property via property window is not getting updated until I build the application once again. What could be the problem?
Say for example. I have a control called Shapes. If I select type of the shape as "Octane" it should show a sample octane in blend design-time surface.
But, in my case it is not happening, the blend designer is not getting updated untill I build the application again. Please advice me on this.
I don't want to put the consumer in trouble by letting them build it for every change in property value they make.
Note: All the exposed properties in the control are dependency property.
Have you implemented the setter of your property so that the controls update when the property's value is changed?
(BTW, just because I'm curious, what's an octane shape? does it have something to do with chemistry?)
What I had was a Properties of type CommonStyles for applying styles. For example,
CommonStyles will contains dp's like Background, Foreground, Thickness, etc.,
The mistake I done was, I directly assigned the values like below. In base class. [ShapeStyle is a dp of type CommonStyle]
//// Both properties are dp's but, assigned them like normal property. This caused the issue
ShapeBase.Background = this.Shape.ShapeStyle.Background;
ShapeBase.Foreground = this.Shape.ShapeStyle.Foreground;
ShapeFace.Background = this.Shape.ShapeFaceStyle.Background;
...
When, I change the background property it won't update my ShapeBase.Background property. Since, it is not dependency bound.
I resolved it by, dp binding. Like below.
this.ShapeBase.SetBinding(BackgroundProperty,
new Binding() {
Source = this.Shape.ShapeStyle,
Path = new PropertyPath("Background") });