【问题标题】:Customize Datagrid columns visibility自定义 Datagrid 列的可见性
【发布时间】:2011-09-07 06:27:29
【问题描述】:

我正在使用 XML 文件中的数据加载我的数据网格,如下例所示。我的 XML 文件包含 4 列数据,这些数据显示在我的数据网格中。但是,我希望数据网格仅显示基于我提供的 ArrayList 的某些列。在此示例中,ArrayList 由两列组成,即“Id”和“Name”。我想知道做我想做的最好的方法是什么。因为我已经加载了网格,我想遍历列并检查列名是否包含在列表中,如果不可见,我将其宽度设置为零。

Or is there another way whereby before loading the grid itself I can do checks between the datagrid dataProvider and the ArrayList, and then populate the grid accordingly. So here the visible attribute will not be used.

Anyone who can put some light on this?


MY MXML
 <?xml version="1.0" encoding="utf-8"?>
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
        <fx:Declarations>
            <fx:XML id="order"  source="orderlist.xml"/>
            <s:XMLListCollection id="show" source="{order.order}"/>
        <s:ArrayList id="mylist">
            <String>Id</String>
                <String>Name</String>
    </s:ArrayList>
        </fx:Declarations>
        <mx:DataGrid dataProvider="{show}" rowCount="4">
            <mx:columns>
                <mx:DataGridColumn headerText="Order number" dataField="Id" />
                <mx:DataGridColumn headerText="Name" dataField="Name" />
                <mx:DataGridColumn headerText="Surname" dataField="Surname" />
                <mx:DataGridColumn headerText="Age" dataField="Age"/>
            </mx:columns>
        </mx:DataGrid>

        <s:Button id="test_btn" click="Handler_to_set_DatagridColumns();"/>


    </s:Application>



MY XML FILE

    <?xml version="1.0" encoding="utf-8"?>
    <Orderlist>
        <order Id="1" Name="Albert" Surname="Schineider" Age="45"/>
        <order Id="2" Name"Sara" Surname="Gutierrez" Age="25"/>
        <order> Id="3" Name="Alain" Surname='Bulquee" Age="40"/>    
    </Orderlist>



Thanks for your help.

【问题讨论】:

    标签: xml datagrid flex4


    【解决方案1】:

    公共类 MyDataGridTextColumn : DataGridTextColumn { #region public Visibility MyVisibility

    public static readonly DependencyProperty MyVisibilityProperty =
        DependencyProperty.Register("MyVisibility", typeof(Visibility), typeof(MyDataGridTextColumn), new PropertyMetadata(Visibility.Visible, OnMyVisibilityPropertyChanged));
    
    private static void OnMyVisibilityPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var @this = d as MyDataGridTextColumn;
    
        if (@this != null)
        {
            @this.OnMyVisibilityChanged((Visibility)e.OldValue, (Visibility)e.NewValue);
        }
    }
    
    private void OnMyVisibilityChanged(Visibility oldValue, Visibility newValue)
    {
        Visibility = newValue;
    }
    
    public Visibility MyVisibility
    {
        get { return (Visibility)GetValue(MyVisibilityProperty); }
        set { SetValue(MyVisibilityProperty, value); }
    }
    
    #endregion public Visibility MyVisibility
    

    }

    【讨论】:

      猜你喜欢
      • 2018-07-15
      • 2011-12-04
      • 2018-07-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 2010-12-01
      • 1970-01-01
      • 2011-04-28
      相关资源
      最近更新 更多