Find an element in DataTemplate applied to TabItem

爷,独闯天下 提交于 2019-12-02 12:28:25

You don't want to use any of the template properties of the TabItem, since those are used to create the actual controls, rather than storing them. You should be able to search the visual tree for the ListView directly, rather than going through the DataTemplate.

Ok, here we come :) I resolve the problem, in not very nice way, but it seems that works correctly. As I mentioned above I used LoadContent method and it returns me the ListView object, but by the way it wasn't the ListView that UI actually uses. So to resolve that problem I add static property to hold my REAL ListView object (static as I have single DataTemplate that contains ListView shared across multiple TabItems, so the ListView shared too) and add event handler to my DataTemplate -> Loaded. Catching this event, that in my case raises only ones in lifetime of application, in RoutedEvent's OriginalSource I got the REAL ListView object that WPF engine uses for rendering on UI. Hope my solution will help someone. Thank you all.

Mohamed Abdo

Simply, if you have a DataGrid, and a TemplateColumn which contains a data template, you can use the following code sample:

<DataGridTemplateColumn x:Name="photoPathColumn" Header="{x:Static resx:FrmResource.Photo}" Width="Auto">
    <DataGridTemplateColumn.CellEditingTemplate x:Uid="keyelm">
        <DataTemplate x:Name="dodo">
            <StackPanel Orientation="Horizontal" Height="Auto">
                <TextBlock x:Name="photo" x:Uid="imageFile" Text="{Binding Path=PhotoPath}"></TextBlock>
                <Button x:Name="Browse" Content="..." Click="Browse_Click"></Button>
            </StackPanel>
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>

TextBlock tBlock = (TextBlok)photoPathColumn.CellEditingTemplate.FindName(
                       "photo",
                       photoPathColumn.GetCellContent(CustomersDataGrid.CurrentItem));
  • Where photo is the name of text block
  • Where photoPathColumn is the DataGrid's TemplateColumn.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!