How to get instance of the GridControl in LookupEd

2019-07-19 03:02发布

问题:

Not a lot to say, just want the instance of DXGrid where i have a LookUpEdit. I'm using WPF.

mylookupedit1.GridControl <-- ???

EDITED :

Here is some sample code:

 <UserControl.Resources>
    <ControlTemplate x:Key="gridTemplate">
        <dxg:GridControl x:Name="PART_GridControl">    
            <dxg:GridControl.View>
                <dxg:TableView Name="view" 
                   AutoWidth="False" 
                   BestFitMode="AllRows" 
                   BestFitArea="All" 
                   AllowBestFit="True"/>
            </dxg:GridControl.View>
        </dxg:GridControl>
    </ControlTemplate>
</UserControl.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="6"/>
        <RowDefinition Height="auto "/>
    </Grid.RowDefinitions>

    <dxe:ButtonEdit Grid.Row="0" Name="beSearch"/>
    <dxg:LookUpEdit Grid.Row="2" Name="leLookUp" 

                ShowSizeGrip="True"
                SelectedIndex="0"
                AutoPopulateColumns="True"

                IsPopupAutoWidth="True"

                ItemsSource="{Binding}"
                PopupMaxWidth="600"   
                PopupContentTemplate="{StaticResource gridTemplate}"    
                  />         

</Grid>

and in my code i have:

 private void mymethod(IEnumerable itemsSource)
        {
            leLookUp.ItemsSource = itemsSource;
            object  o = leLookUp.FindParentOfType<GridControl>();
            // o is null 
            // how could i access PART_GridControl ???
        }

回答1:

You should be able to just use the GetGridControl function.

DevExpress GetGridControl Link

Otherwise (ideally the above works) you can use the following, I use this for some places where I need to find Parents etc.

        private static DependencyObject FindParent(this DependencyObject obj, Predicate<DependencyObject> where)
        {
            var parent = VisualTreeHelper.GetParent(obj);

            if (parent == null || where(parent))
            {
                return parent;
            }

            return parent.FindParent(where);
        }

        public static T FindParentOfType<T>(this DependencyObject obj) where T : DependencyObject
        {
            return (T) FindParent(obj, x => x is T);
        }

So then you could just go:

var grid = mylookupedit1.FindParentOfType<GridControl>();

EDIT:

As I misunderstood the question here is the other approach to get the child.

I've previously tried many different ways to do this, however none worked, I've tried going through all children etc etc. But none actually got the GridControl. So what we've done is the following:

In the declaration of your GridControl, add a Loaded event:

<dxg:GridControl Name="PART_GridControl" Loaded="LoadedEvent">

Then in your code behind, create a variable to store the grid:

private GridControl theGridInTheControlTemplate;

And then you can implement the LoadedEvent handler:

private void LoadedEvent(object sender, RoutedEventArgs e)
{
   theGridInTheControlTemplate = (GridControl)sender;
}

so now you can use theGridInTheControlTemplate in your code.

I know it doesn't seem too clean, but it's the only way I found this to work.

Hope this helps, Richard



回答2:

Here are better ways.

    void edt_PopupOpened(object sender, RoutedEventArgs e)
    {
        LookUpEdit edt = (LookUpEdit)sender;

        Dispatcher.BeginInvoke((Action)(() =>
        {
            GridControl t = edt.GetGridControl();
           // do something with a column... t.Columns["RecId"].Visible = false;
        }), DispatcherPriority.Input);
    }

or

    void YourGridNameifItsInGrid_ShownEditor(object sender, EditorEventArgs e)
        {
            if (e.Editor is LookUpEdit)
            {
                LookUpEdit lookupEdit = (LookUpEdit)e.Editor;
                GridControl gGridControl = lookupEdit.GetGridControl();                  
            }
        }