WPF DataTrigger on PropertyChanged - c#

I have a view model property that is set to runtime objects. I want to trigger an animation whenever this property changes, so I was planning to use DataTrigger. However, DataTrigger obviously has the requirement for a Value property--one that I don't know at design-time.
Is there a built in way to trigger an animation whenever a value changes, regardless of what it changes into?
I saw this question but I was wondering if there was anyway to do it purely in XAML. Otherwise I figure I could probably fire an event from my View Model whenever the property changes and listen to that.

One method would be to create a User Control with a dependency property and then bind both of your other properties to that i.e. one at compile time and the other at runtime. Alternatively you could use an Attached Behaviour to do the same thing.

Can add a boolean property and trigger the animation based on the bool property. Whenever the original property changes, set and reset the boolean property so that it triggers the animation and also goes back to default value for next notification.

Related

Do i have to implement all usercontrol properties by interface in WPF?

I'm programming a bunch of WPF usercontrols (not CommonControls!) using an Interface for the common properties like the alignments or the Content (depending on the control; TextBox returns the Text, and Label the Content per example).
Now i get to controls like the Checkbox, which have the not so common property IsChecked.
Do i have to implement this property by the Interface or is it allowed to program it in the usercontrol itself?
I hope you're talking about binding a property to a view model or directly mentioning the property value in the xaml itself.
It actually depends upon your requirement. We normally bind a property value to a view model or code behind when that property needs to be checked by certain conditions and then to be set. If you're sure of the property's value, you can set it in xaml itself.
For example if you want to set a textbox's IsEnabled property, and you're sure that the text box is always editable. Then set it as true in xaml itself. But if you're text box need to be enabled during an event is handled, bind it to a property in view model and set it to true during the event trigger.
Okay, i have to guess it was a dumb question.
But after eight hours you don't know anymore, if you're Hillary or Donald.
The answer is: An Interface can inherit from another and implement all members from the "parent" interface.

Binding triggered by property changed and by validation

I've been using the .Net binding in Win Forms. I would like the bound property to be updated as soon as the user alters the value in the corresponding control. To achieve this I have set the Data Source Update Mode to OnPropertyChanged. Unfortunately, when i navigate away from the page the Focus Changed event causes Validation to occur again, which also causes the value to be set. Is there anyway to prevent this or work around it (other than setting the update mode to never) and pushing update manually?
Seems i just needed to set CausesValidation to false on all the related controls

WPF C# Textbox text change update in ViewModel

Hello I am working on a simple MVVM project; a simple text/config editor that loads a config file and then it checks in the ViewModel in case the file has been changed, it enables the Save menu item simply by binding a boolean property. But here comes a problem, where I can't find any property in the textbox control that could be bound to a vm property in case a change happens in the text. I have managed to somehow simulate this by creating an event in the code-behind :
(DataContext as AnalizeSectionViewModel).ContentChanged = true;
The event is fired on any text change. But I would like to bind a property from the textbox, something like:
IsModified="{Binding ContentChanged}"
Can such a thing be done?
You should be able to just bind the Text textbox property to your model via binding
Text="{Binding MyViewModelProperty}"
Anytime the text in your textbox changes your property in your model will change which will allow you to do 'stuff' when that occurs. This will fire the property changed event when the user moves to out of the field.
Now, if the intent is for it to fire each time the user types then you can explicitly tack on the
UpdateSourceTrigger="PropertyChanged"
By setting it to PropertyChanged, you will get a notification each and every time the text changes.

EF object property changing - MVVM

I have a viewmodel that has an EF object as one of its properties. The view has several text boxes wired up to that property via Text="{Binding Path=MyEFTable.Column1}". I'm using the MVVM Light toolkit so my viewmodels inherit ViewModelBase and subsequently each property calls RaisePropertyChanged() when changed.
I have noticed, however that changing MyEFTable.Column1 does not set off any changes in the viewmodel. I thought that EF objects tracked any changes, so I assumed that changing a column value in an EF object would cause the EF object itself to be changed. Is it no longer tracking changes since I am essentially copying the query result into a new MyEFTable object?
Does your bindable objects implement INotifyPropertyChanged or it's container, like ObservableCollection?
Are you rising the event?
Using a view model is only the start. First thing you should check is to see if the Column1 Property is being updated after the editing occurs. If a TextBox, the UpdateSourceTrigger default value is LostFocus. Maybe you can try change it to PropertyChanged.
If the value is updated in your MyEFTable.Column1 you're done. If not, again, something is using the INotifyPropertyChange.
Add more info and maybe the issue will be clearer.
HTH

Setting textBox.Text doesn't update bound twoway property?

Does it make sense that if the Text on a TextBox is databound to a property using the twoway mode and I set the Text to something, it should update the property? My property gets updated when I type inside the control, but not when I set the value in code.
I would say it makes no sense to modify a bound Text property directly. Your code should be setting the other end of the binding and allowing the binding to update the control.
If the bound object is updated when the Text property is set then special case code would be needed to detect when such an assignent is the result of the bound object changing for other reasons. Otherwise you would end up with an infinite loop.
You shouldn't set the .Text value of the textbox... set the value of the property it's binding to. :)
I'd encourage you to read more about the Model-View-ViewModel method for designing your views. It keeps a clear separation of concerns when doing this kind of work. The reason you are seeing this "bug" regarding focus causing the binding to refresh is because most of the time this kind of thing is not appropriate.
Here's a pretty good video introduction to MVVM: MVVM on Channel 9
This is because it only commits the data when the textbox loses focus. Here is a question that is somewhat related that eludes to this.

Categories

Resources