Note that there are some explanatory texts on larger screens.

plurals
  1. POFind an element in DataTemplate applied to TabItem
    primarykey
    data
    text
    <p>I got a problem trying to find an element declared in DataTemplate, that after was applied like a ContentTemplate to TabItem object. I saw that there is already some solutions in regard of this problem, but no one of them actually works in my case, and I would like to understand why (obviously I make mistake in some place) Here is a sample code:</p> <pre><code>&lt;DataTemplate x:Key="TabItemDataTemplate"&gt; &lt;Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="templateGrid"&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="6.0*"&gt; &lt;/RowDefinition&gt; &lt;RowDefinition Height="6" &gt;&lt;/RowDefinition&gt; &lt;RowDefinition Height="6.0*" &gt;&lt;/RowDefinition&gt; &lt;RowDefinition Height="*" &gt;&lt;/RowDefinition&gt; &lt;/Grid.RowDefinitions&gt; &lt;ListView x:Name="repoView" Grid.Row="0" VerticalAlignment="Stretch" ItemsSource="{Binding Source={StaticResource DataProviderForListView}}"&gt; &lt;GridView&gt; &lt;GridViewColumn Header="State" DisplayMemberBinding="{Binding Path=RepositoryItemState}"/&gt; &lt;GridViewColumn Header="Working Copy Rev num." DisplayMemberBinding="{Binding Path=WCRevision}"/&gt; &lt;GridViewColumn Header="Repository Rev num." DisplayMemberBinding="{Binding Path=RepoRevision}"/&gt; &lt;GridViewColumn Header="User" DisplayMemberBinding="{Binding Path=Account}"/&gt; &lt;GridViewColumn Header="Item" DisplayMemberBinding="{Binding Path=ItemName}"/&gt; &lt;/GridView&gt; &lt;/ListView&gt; &lt;GridSplitter x:Name="gridSplitter" Grid.Row="1" ResizeDirection="Rows" Background="Gray" Height="4" HorizontalAlignment="Stretch" Style="{StaticResource gridSplitterStyle}"/&gt; &lt;RichTextBox x:Name="rowView" Grid.Row="2" BorderBrush="Bisque" VerticalAlignment="Stretch" IsReadOnly="True" Background="YellowGreen" FontFamily="Comic Sans Serif"/&gt; &lt;ToggleButton x:Name="rbWorkingCopy" Template="{StaticResource ToggleButtonControlTemplate}" Grid.Row="3" Width="100" Height="22" Content="{StaticResource WorkingCopyTitle}" HorizontalAlignment="Left" VerticalAlignment="Bottom" Command="repoManager:AppCommands.GetWorkingCopyInfoCommand" /&gt; &lt;ToggleButton x:Name="rbRepository" Template="{StaticResource ToggleButtonControlTemplate}" Grid.Row="3" Width="100" Height="22" Content="{StaticResource RepositoryTitle}" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="120,0,0,0" Command="repoManager:AppCommands.GetRepoInfoCommand" /&gt; &lt;ProgressBar x:Name="checkRepositoryProgress" Grid.Row="3" Width="220" Height="22" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="250,0,10,0" IsIndeterminate="True" IsEnabled="{Binding repoManager:ExecutingCommand}" /&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; </code></pre> <p>This code is porgrammatically applied to the given TabItem object in following way : </p> <pre><code>this.ContentTemplate = FindResource("TabItemDataTemplate") as DataTemplate; </code></pre> <p>After I need access to the ListView element declared in DataTemplate, so I execute the codes found around in internet, and also on this site. Here is a short example:</p> <pre><code>/* Getting the ContentPresenter of myListBoxItem*/ ContentPresenter myContentPresenter = FindVisualChild&lt;ContentPresenter&gt;(this); // this.GetVisualChild(0) /* Finding textBlock from the DataTemplate that is set on that ContentPresenter*/ DataTemplate myDataTemplate = myContentPresenter.ContentTemplate; ListView repoListView = (ListView)myDataTemplate.FindName("repoView", myContentPresenter); </code></pre> <p>Problem1: In this case <strong>ContentTemplate</strong> of ContentPresenter is Null, so code execution crashes. Prolem2: Ok, I think, may be I need to navigate throw TabItem content directly, so the code becomes, more or less:</p> <pre><code>/* Getting the ContentPresenter of myListBoxItem*/ ContentPresenter myContentPresenter = FindVisualChild&lt;ContentPresenter&gt;(this); // this.GetVisualChild(0) /* Finding textBlock from the DataTemplate that is set on that ContentPresenter*/ DataTemplate myDataTemplate = this.ContentTemplate; ListView repoListView = (ListView)myDataTemplate.FindName("repoView", myContentPresenter); </code></pre> <p><strong>this</strong> is TabItem object. But the strage things, that the ContentTemplate of <strong>this</strong> is completely different from that one assigned above. I'm sure that I missed something somewhere, can you help me to figure out the problem ? Thank you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload