如何在Flex中处理非可视对象的事件

时间:2011-11-07 11:52:23

标签: actionscript-3 flex events

我正在尝试执行双向绑定,例如我有一个按钮(在许多控件中),在其选择上,我在某些textinput中显示其diff属性的值(如高度,宽度等)。这种单向处理工作正常。

但反向过程不起作用。即当我选择一些按钮,并尝试通过在高度,宽度textinputs中输入一些值来改变其尺寸时,尺寸不会改变。

如何知道我选择了哪个按钮?如何在这里处理事件?


private void Form1_Load(object sender, System.EventArgs e)
        {
            //Create some data and bind it to the grid
            dt1 = GetData(1000, 3);
            this.UltraGrid1.DataSource = dt1;
            //Set the grid's CreationFilter to a new instance of the NumbersInRowSelectors class. 
            this.UltraGrid1.CreationFilter = new NumbersInRowSelectors();           
        }

        private void UltraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {       
            //Hide the default images that are drawn in the RowSelectors, like the pencil and asterisk, etc. 
            e.Layout.Override.RowSelectorAppearance.ImageAlpha = Infragistics.Win.Alpha.Transparent;

            //Center the text in the RowSelectors.
            e.Layout.Override.RowSelectorAppearance.TextHAlign = Infragistics.Win.HAlign.Center;
            e.Layout.Override.RowSelectorAppearance.TextVAlign = Infragistics.Win.VAlign.Middle;           

            //There is no wy to change the width of the RowSelectors.
            //Use a smaller font, so that 3-digit numbers will fit. 
            e.Layout.Override.RowSelectorAppearance.FontData.Name = "Small Fonts";
            e.Layout.Override.RowSelectorAppearance.FontData.SizeInPoints = 6;
        }

        //The NumbersInRowSelectors class. This class Implements a CreationFilter and
        //adds a TextUIElement to each RowSelector which displays the row number of
        //the row.
        public class NumbersInRowSelectors:Infragistics.Win.IUIElementCreationFilter
        {

            #region Implementation of IUIElementCreationFilter
            public void AfterCreateChildElements(Infragistics.Win.UIElement parent)
            {
                //Don't need to do anything here
            }
            public bool BeforeCreateChildElements(Infragistics.Win.UIElement parent)
            {
                //Declare some variables
                Infragistics.Win.TextUIElement objTextUIElement;
                Infragistics.Win.UltraWinGrid.RowSelectorUIElement objRowSelectorUIElement;
                Infragistics.Win.UltraWinGrid.UltraGridRow objRow;
                int RowNumber;

                //Check to see if the parent is a RowSelectorUIElement. If not, 
                //we don't need to do anything
                if (parent is Infragistics.Win.UltraWinGrid.RowSelectorUIElement)
                {
                    //Get the Row from the RowSelectorsUIElement
                    objRowSelectorUIElement = (Infragistics.Win.UltraWinGrid.RowSelectorUIElement)parent;
                    objRow = (Infragistics.Win.UltraWinGrid.UltraGridRow)objRowSelectorUIElement.GetContext(typeof(Infragistics.Win.UltraWinGrid.UltraGridRow));

                    //Get the Index of the Row, so we can use it as a row number. 
                    RowNumber = objRow.Index;

                    //Check to see if the TextUIElement is already created. Since
                    //The RowSelectorsUIElement never has children by default, we
                    //can just check the count. 
                    if (parent.ChildElements.Count == 0)
                    {
                        //Create a new TextUIElement and parent it to the RowSelectorUIElement
                        objTextUIElement = new Infragistics.Win.TextUIElement(parent, RowNumber.ToString());
                        parent.ChildElements.Add(objTextUIElement);
                    }
                    else
                    {
                        //There's already a TextUIElement here, so just set the Text
                        objTextUIElement = (Infragistics.Win.TextUIElement)parent.ChildElements[0];
                        objTextUIElement.Text = RowNumber.ToString();
                    }
                    //Position the TextUIElement into the RowSelectorUIElement
                    objTextUIElement.Rect = parent.RectInsideBorders;

                    //Return True let the grid know we handled this event. 
                    //This doesn't really do anything, since the grid
                    //does not create any child elements for this object, anyway.
                    return true;
                }

                //Return false to let the grid know we did not handle the event. 
                //This doesn't really do anything, since the grid
                //does not create any child elements for this object, anyway.
                return false;               
            }       
            #endregion
        }

    }

1 个答案:

答案 0 :(得分:0)

在声明按钮和文本编辑的类中创建“当前选定的项目”成员。

在按钮选择事件监听器中,将事件目标分配给该成员。然后在文本编辑事件监听器中使用它。 例如:

// It's a declaration of the member variable
private var m_current_btn:Button = null;

// It's an event listener for your button
private function on_selection_change(event:Event):void
{
   m_current_btn = event.target as Button;
   // button_x and button_y are two text edits
   button_x.text = m_current_button.x.toString();
   button_y.text = m_current_button.y.toString();
}

// Event listener to track changes in the coordinate text inputs
private function on_coordinate_textedit_change(event:Event):void
{
   if (m_current_btn != null)
   {
     m_current_btn.x = parseInt(button_x.text);
     m_current_btn.y = parseInt(button_y.text);
   }
}