Obtain DataGrid inside ItemsControl from the binded item

你离开我真会死。 提交于 2019-12-02 13:46:37

问题


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>

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

ObservableCollection<Dictionary<string, string>> Dists;

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);

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


回答1:


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;
}

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

}


来源:https://stackoverflow.com/questions/26049841/obtain-datagrid-inside-itemscontrol-from-the-binded-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!