質問

I have a problem with my application in flex4, i have this code for my button:

<buttons:MyButton id="btnNewPost"
    label="{I18nManager.getInstance().main.newPost}"
    minWidth="170"
    click="{doSomething()}"
    creationComplete="btnNewPost_creationCompleteHandler(event)"/>

And on creation complete i have this code:

if (this.newItemButtonEnabledWithCategories)
{
    BindingUtils.bindProperty(this.btnNewPost, "enabled", ModelLocator.getInstance(), "currentCategory");
}

Well, so this is clear, when property currentCategory is not null, then the button will be enabled.

What i want:

I want to dind the property enable to another property of the ModelLocator, itemMode but only if itemMod is ready

I tried BindingUtils.bindProperty(this.btnNewPost, "enabled", ModelLocator.getInstance(), "itemMode"); without luck because if itemMode has any value then the property enabled is always true

Is there any way to only enable item if itemMode is ready?

役に立ちましたか?

解決

You might want to use bindSetter rather than bindProperty. This allows you to have a method handle when the property has changed:

BindingUtils.bindSetter(itemModeChanged, 
                        ModelLocateor.getInstance(), 
                        "itemMode");

protected function itemModeChanged(mode:String):void
{
    this.btnNewPost.enabled = (mode == "ready");
}

(As another thought, is there any reason you can't specify the binding in the MXML? enabled="{ModelLocator.getInstance().itemMode == 'ready'} I haven't tried this exact example to verify it works, but I think it should.)

他のヒント

You could bind to a new property, itemModeIsReady in your ModelLocator. In your setter for itemMode, dispatch a PropertyChangeEvent for itemModeIsReady and have an appropriate getter:

public function set itemMode(mode:String):void {
  var previousValue = _itemMode;
  //existing 
  dispatchEvent(PropertyChangeEvent.createUpdateEvent(this, "itemModeIsReady", previousValue == "ready", mode == "ready"));
}

[Bindable]
public function get itemModeIsReady():Boolean {
  return itemMode == "ready";
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top