Attached properties Clearing local value but xaml not refreshing - wpf

I derived the StackPanel class in a OrderableStack Class.
My aim is to allow a Stack's child to set an attached property Named 'First' to true and so this child becomes the first element in the stack.
When the attached property is set to true, the PropertyChangedCallback procedure defined in Metadata is called and it first removes the child element from the stack , then inserts it at position 0. Finally all the other children have their 'First' attached property set to False.
It works , but :
-if the attached property is already defined in xaml when i display it in VS 2010, VS 2010 is indicating that 'a reference is not set...'
-i can check that every other child, except the one defined as 'first' , has its OrderableStack.First set to false in the Properties Window, but the xaml is not updated. So many children can have the OrderableStack.First attached property set to true (in XAML), even if actually only the last child to be set has the True value.
Could you help me?

if the attached property is already defined in xaml when i display it in VS 2010, VS 2010 is indicating that 'a reference is not set...'
Does this stop you from building and running your app? The way you can avoid this "designer" error is to put some nullability checks around your attached property's PropertyChangedCallback code.
i can check that every other child, except the one defined as 'first'
, has its OrderableStack.First set to false in the Properties Window,
but the xaml is not updated.
I didnt get that last bit. Do you expect your XAML designer to "simulate" the ordering in design mode? Then the best candidate designer is XAML Pad or Expression Blend. Visual Studio designers are sadly not the best out there. They also do not "simulate" animations / triggers.
I hope this helps.

Related

Changing the default setting of a WinForms control property

I seek to change the default setting of a .NET Windows Forms control's property, without creating a custom control inheriting from it.
What I want is, for example, to add a TextBox on my Form that already has the TextAlign property set to HorizontalAlignment.Right instead of HorizontalAlignment.Left. Even if only solution-wide, if achievable, I would love to know. This would save a lot of time for when one is working with a LOT of controls and needs to set their properties to specific, non-default, values.
Creating a custom control is just too overkill for this, and would clutter my solution with unnecessary things. I have also considered running a regex on the designer files solution-wide (to add the non-default values to such controls), but writing regex like that can (also) be time consuming/problematic.
Any ideas?
There is a way to do that. But you need to set the binding initially. Later on you can set in one go to all the controls of your choice. This might be not your exact answer but this is how we define styles like as in WPF
(1) Click the Application Property Bindings
(2) Define Binding
(3)Now for textbox you can set the initial default
(4) Now You can set the property binding of every textbox intially or you can code which will set the application settings at starup or on form load.
This will you help to change the default propety at once later on

wpf:attached properties

IS attached properties enable to modify the properties of a root element or parent element in the logical tree in any one of the child .
Just wanted to check whether I have understood it right ?
Well an attached property is nothing more than a value you can attach to a DependencyObject. You can however handle value changed, of the attached property, in order to run your specific logic (this is also called attached behaviors).
The short answer yes.

WPF, MVVM, and ComboBox: when changing to a different viewmodel, the ComboBox nulls the property bound to SelectedItem

I have a Window that uses DataTemplates to display a different UserControl (view) in a ContentPresenter based on the type of its Content property, which is bound to a property that holds the current viewmodel. In this way, by changing the viewmodel property with an event, I can facilitate the basic back/forward navigation I need.
When creating a new viewmodel, it is passed a reference to the current one. Going back to the old viewmodel instance works fine for a CheckBox control, but not for a UserControl I made that contains a TextBlock and a ComboBox.
The problem is that, when the view containing the ComboBox gets unloaded, the ComboBox's ItemsSource gets nulled, which triggers it to clear its SelectedItem/Text properties, which are for some reason still bound to my viewmodel--thus clearing the data it stores. I don't know how to manually unbind them at the appropriate time. (Again, the CheckBox works just fine.)
I have read that other users have had this exact same problem. For them, changing the declaration order of the ItemsSource and SelectedItem/Text bindings so that the attributes for the latter are placed before the former fixes the issue. However, in my case, it does not. Others have also fixed the issue by ignoring null/empty values, but this won't work in my case.
I could work around the issue by copying the interesting data to a separate object, and reloading it from that, but I would need to add code to trigger reloading the data = more data linkage code to maintain.
I could also avoid using DataTemplates and manually add the UserControls in the codebehind, which would allow me to break the data binding before removing the UserControl. But this runs counter to the point of MVVM.
I'm not above modifying my very non-MVVM UserControl to handle any events on the ComboBox it contains to work around this issue.
UPDATE:
I have narrowed down the issue a little bit. I refactored the code so that it manually creates and adds the view UserControl based on which viewmodel has been set. The issue now only occurs when I set the DataContext of the view UserControl to null. If I simply replace the view without removing the reference, it no longer erases the values in question. Is this a usable workaround, or does it create issues like memory leaks?
Maybe something that would "open mind" for a simpler solution... If I understand your problem, it's similar to a past problem we had. In our case, we simply made the assumption that it's not possible to set a specific value to null when accessed by the bound property, so we tweaked the appropriate ViewModel Properties a bit:
public MyItem SelectedItem {
get {
return Model.MyItem;
}
set {
if (value != null) {
// Set and notify if not null
Model.MyItem = value;
OnPropertyChanged("SelectedItem");
}
else // just notify when trying to set to null
OnPropertyChanged("SelectedItem");
}
}
Using such tweaked properties we were able to block any try to set the value to null, by calling OnPropertyChanged(..) insead, the existing value was recalled by the UI. If there is a need to be able to set a value to null, you have to provide a seperate property allowing that.
Not sure if this applies to your problem.
Good luck.
UPDATE
oh, I see probably this describes same method as "Others have also fixed the issue by ignoring null/empty values" which seems not to work in your case. But I dont unterstand why it shouldn't.
This is a known bug in the early versions of WPF caused by event leapfrogging. It was fixed for the Selector-derived controls in .NET 4.0.
See this blog post for more details: http://blogs.interknowlogy.com/2011/03/09/event-leapfrogging/
I have worked around the issue by adding a property Active and corresponding Activate()/Deactivate() methods to my base viewmodel class, and calling these as appropriate when swapping out viewmodels. This fits into my application pretty well. I'm still open to other suggestions, of course.

Blend - PropertyChanges are not getting updated

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") });

WPF: Activate Trigger when a MVVM bound property changes

somehow I am going in circles here. Please forgive me if the answer to this question is obvious.
I want to react to changed properties in the ViewModel in the View. When the properties (bool) change the View should start an animation (BeginStoryBoard).
Actually in my application there are 4 of these properties each with its own name in the VM and two desired animations each (hide/show) for the respective 4 container view elements.
When setting these Triggers (tried DataTrigger/Trigger/EventTrigger) directly in the respective container elements (all of them of custom type SizerControl derived from ContentConrol) wpf gave me errors suggesting using a style. When setting a style directly in the container elements I got: "The tag 'Style.Triggers' does not exist in XML namespace ..."
But when using a style in a global ResourceDictionary I am at a loss how to bind to the 4 containersto get the "input" value for the trigger.
Do you have a suggestion how to solve this from an architecture pov? Maybe I am already down too far the wrong path to see the obvious solution.
Thanks a bunch,
Hinnerk
You can do this with a DataTrigger and a StoryBoard.
MSDN has a full sample describing the process, with code, documented in How to: Trigger an Animation When Data Changes.

Resources