从绑定的项目获取ItemsControl内的DataGrid [英] Obtain DataGrid inside ItemsControl from the binded item

查看:88
本文介绍了从绑定的项目获取ItemsControl内的DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ItemsControl,它在其模板中使用DataGrid,如下所示:

I have an ItemsControl that uses DataGrid in its template like this:

<ItemsControl Name="icDists" ItemsSource="{Binding Dists}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <DataGrid ItemsSource="{Binding}" Width="150" Margin="5" AutoGenerateColumns="False" IsReadOnly="True">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Key" Binding="{Binding Key}" Width="1*" />
                    <DataGridTextColumn Header="Value" Binding="{Binding Value}" Width="1*" />
                </DataGrid.Columns>
            </DataGrid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

ItemsControl绑定到我的模型中的Dists属性,如下所示:

The ItemsControl is binded to a Dists property in my model that looks like this:

ObservableCollection<Dictionary<string, string>> Dists;

如何获取与Dists属性中的项目相对应的DataGrid?我已经尝试过使用此代码,它为我提供了ContentPresenter,但我不知道如何从中获取DataGrid:

How can I obtain the DataGrid that corresponds to an item in the Dists property? I've tried with this code, which gives me a ContentPresenter but I don't know how to obtain the DataGrid from it:

var d = Dists[i];
var uiElement = (UIElement)icDistribucion.ItemContainerGenerator.ContainerFromItem(d);

我尝试用VisualHelper.GetParent在树上行走,但是找不到DataGrid.

I've tried walking up the tree with VisualHelper.GetParent but couldn't find the DataGrid.

推荐答案

如果要执行类似的操作,则需要搜索VisualTree.尽管我建议阅读更多有关MVVM模式的信息.但是,这就是您想要的.

Need to search the VisualTree if you want to do something like that. Though I recommend reading a bit more on MVVM patterns. But here is what you want.

using System.Windows.Media;

private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
    var count = VisualTreeHelper.GetChildrenCount(parentElement);
    if (count == 0)
        return null;

    for (int i = 0; i < count; i++)
    {
        var child = VisualTreeHelper.GetChild(parentElement, i);

        if (child != null && child is T)
        {
            return (T)child;
        }
        else
        {
            var result = FindFirstElementInVisualTree<T>(child);
            if (result != null)
                return result;
        }
    }
    return null;
}


现在,在设置ItemsSource并准备好ItemControl之后.我将在Loaded事件中进行此操作.


Now after you set your ItemsSource and the ItemControl is ready. I'm just going to do this in the Loaded event.

private void icDists_Loaded(object sender, RoutedEventArgs e)
{
    // get the container for the first index
    var item = this.icDists.ItemContainerGenerator.ContainerFromIndex(0);
    // var item = this.icDists.ItemContainerGenerator.ContainerFromItem(item_object); // you can also get it from an item if you pass the item in the ItemsSource correctly

    // find the DataGrid for the first container
    DataGrid dg = FindFirstElementInVisualTree<DataGrid>(item);

    // at this point dg should be the DataGrid of the first item in your list

}

这篇关于从绑定的项目获取ItemsControl内的DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆