本文介绍了如何在LookupEdit(Devexpress)中获取GridControl的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不算太多,只需要我有一个LookUpEdit的DXGrid的实例。
我正在使用WPF。



mylookupedit1.GridControl< - -



EDITED:



以下是一些示例代码:

 < UserControl.Resources> 
< ControlTemplate x:Key =gridTemplate>
< dxg:GridControl x:Name =PART_GridControl>
< dxg:GridControl.View>
< dxg:TableView Name =view
AutoWidth =False
BestFitMode =AllRows
BestFitArea =全部
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 =0Name =beSearch/>
< dxg:LookUpEdit Grid.Row =2Name =leLookUp

ShowSizeGrip =True
SelectedIndex =0
AutoPopulateColumns = True

IsPopupAutoWidth =True

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

< / Grid>

在我的代码中我有:

  private void mymethod(IEnumerable itemsSource)
{
leLookUp.ItemsSource = itemsSource;
object o = leLookUp.FindParentOfType< GridControl>();
// o是null
//如何访问PART_GridControl?
}


解决方案

你应该能够使用GetGridControl函数。





否则(最理想的是上述工作),您可以使用以下内容,我在某些需要找到父母等的地方使用此功能。

  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>(此DependencyObject obj)其中T:DependencyObject
{
return(T)FindParent(obj,x => x是T);
}

所以你可以去:

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

编辑:



当我误解了这里的问题是让孩子得到另一种方法。



我以前尝试过许多不同的方法来做到这一点,但是没有任何效果,我已经尝试过所有的孩子等等,但没有一个实际得到GridControl。所以我们做了以下工作:



在GridControl的声明中,添加一个Loaded事件:

 < dxg:GridControl Name =PART_GridControlLoaded =LoadedEvent> 

然后在你的代码背后,创建一个变量来存储网格:

  private GridControl theGridInTheControlTemplate; 

然后你可以实现LoadedEvent处理程序:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b $
}

所以现在你可以在代码中使用GridInTheControlTemplate。



我知道这看起来不太干净,但这是我发现这个工作的唯一方法。



希望这有帮助,
理查德


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 ???
        }
解决方案

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

这篇关于如何在LookupEdit(Devexpress)中获取GridControl的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 21:48