|
Thanks, Peter,
My dataset is in U.S unit, if user select Metric, I will capture metric input and converted to U.S. units and populate the dataset. The U.S. dataset is passed to model/controller to get the ouptput dataset also in U.S units, before display the result dataset back, if it is in Metric, I need to convert to metric. Currently the dataset is bound to WPF controls, directly e.g. <datagrid> and in U.S unit alone. I am going to look into ValueConverter.
|
|
|
|
|
If you convert them yourself, you'll need to convert all of them. You really only care about the ones the user is looking at. If you have 100,000 items and the user only looks at 100, there is no need to do the computations to convert the rest. The IValueConverter will accomplish this for you. It'll only call in to do the conversion on whats needed by the UI. Small optimization, but can make a huge difference in performance.
|
|
|
|
|
I have a ViewModel that contains a CompanyEntity. In the CompanyEntity is a property called IsActive. This property raises NotifyPropertyChanged. The property is bound to a checkbox in the UI.
What I'd like is to somehow execute a method in the VM when the user changes the IsActive property via the checkbox.
How do I do this?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
There's a couple of ways you could do this. One way is to hook into the property itself and call the method when you're changing the value. The other way is to use RX to observe the property and call the method whenever the property changes. There's a pretty decent primer here[^] that should give you a heads up.
|
|
|
|
|
Pete,
Can you explain what you mean by 'Hook into the property'?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
It's just a case of adding a piece of logic into your property setter and calling your method if the value has changed.
|
|
|
|
|
|
Well I implemented this and it doesn't work. I registered the property, compiled, and ran it. Nothing happens. The method I registered doesn't get called. The property IS changed, but the callback doesn't get called.
Too bad, it looked promising.
[UPDATE]
So here's what I just found....
I have a GridView bound to a list of JobAssignmentEntities and who's SelectedItem is bound to a property in my VM called SelectedJobAssignment.
In the CTOR of the VM I did
propertyObserver = new PropertyObserver<JobAssignmentHeaderEntity>(this.SelectedJobAssignment)
.RegisterHandler(n => n.PhaseStart, n => this.phaseStartChanged(n));
In order to register it the property must not be null, because of a null ref check in the register method.
So I moved the block above inside the SelectedJobAssignment's Getter and it works.
sooo, I will need to find a way to register EACH time the SelectedJobAssignment is set.
If it's not broken, fix it until it is
modified 11-Mar-14 11:59am.
|
|
|
|
|
Yes, this makes sense.
The PropertyObserver constructor attaches to the current value (this.SelectedJobAssignment ) of the passed property.
It does not automatically track if that property itself changes. (I.e., it doesn't notice if this.SelectedJobAssignment changes)
The registration tracks if the referenced property of that value changes. (I.e., it only notices if this.SelectedJobAssignment.PhaseStart changes)
|
|
|
|
|
Why not move it into the SelectedJobAssignment 's Setter ?
|
|
|
|
|
In ref to this and your prior posting....
I see how it works, it just looks kludgy.
First, I could see something like
propertyObserver.Register(typeof(theType), x => x.Property, MyHandler());
this way you can register multiple type, and only need to have this on one place.
Having to re-register each time the property changes is ugly
If it's not broken, fix it until it is
|
|
|
|
|
You just use a different property observer.
One for "the class" that checks when specific properties are changed.
In the handlers for that observers, you register handlers for the properties on the values of the "properties of the class".
This way the PropertyObserver handles the general case.
|
|
|
|
|
Ya I know. THe idea is great, just not the cleanest implementation.
If it's not broken, fix it until it is
|
|
|
|
|
Hi,
Simply you can achieve this by using triggers.
Write an Event Trigger with event name as Click and bind an ICommand property which is in the viewmodel.
So whenever there is a click on the checkbox(either select or deselect this method triggers)
Then you can write the logic according to your functionality.
There are so many ways to achieve this as well.
|
|
|
|
|
I'm trying to bind a window's Activated event to a command:
So I have:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
and
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="..\AppResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Activated">
<i:InvokeCommandAction Command="{Binding WindowActivatedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ResourceDictionary>
</Window.Resources>
I get a compilation error
"Property 'Triggers' is not attachable to elements of type 'ResourceDictionary'"
What's wrong here??
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
Move it outside the Window.Resources section. All you need do is add it after the opening Window tag.
|
|
|
|
|
Thanks Pete. That did it.
The Getter for the command fires, but the execute never does.
My code is pretty standard:
private ICommand _WindowActivatedCommand;
public ICommand WindowActivatedCommand
{
get
{
if (_WindowActivatedCommand == null)
_WindowActivatedCommand = new RelayCommand(p => windowActivated(), p => windowCanActivate());
return _WindowActivatedCommand;
}
}
private bool windowCanActivate()
{
return true;
}
private void windowActivated()
{
}
Neither windowCanActivate nor windowActivated ever fire. How do I know if the Event is calling the command?
If it's not broken, fix it until it is
|
|
|
|
|
How have you assigned your DataContext?
|
|
|
|
|
Yes.
If it's not broken, fix it until it is
|
|
|
|
|
I think you need to read my question again.
|
|
|
|
|
About the DC? Yes, I assigned it.
This window is working. It's just THIS piece that's not working.
If it's not broken, fix it until it is
|
|
|
|
|
I asked how have you assigned the DC, not have you assigned it? It's entirely possible that you have created it after the Activated event has fired.
|
|
|
|
|
oh oh, my bad. I'm sorry. I totally missed the word 'how'
I did it in the code behind after the InitializeComponent.
If it's not broken, fix it until it is
|
|
|
|
|
It's my fault - I just reread your post - the getter is being hit, so the DataContext has been hooked up.
In order to test your Activated event, bring another window to the front and then reselect your application. See if it fires then (it should).
|
|
|
|
|
You're right - it fired AFTER I re-activated it.
It just doesn't fire on startup, which is what I want.
[UPDATE]
According to this[^]
the Window is activated when "The window is first opened."
The Activate command fires AFTER I lose and regain focus, but not on first startup.
[UPDATE 2]
I replaced the command with the event subscription in the code begind and it DOES fire on startup, so for some reason the command isn't firing until later on.
If it's not broken, fix it until it is
modified 3-Mar-14 18:57pm.
|
|
|
|