代码之家  ›  专栏  ›  技术社区  ›  Tigran

在DataTemplate中查找应用于TabItem的元素

  •  0
  • Tigran  · 技术社区  · 15 年前

    我在尝试查找DataTemplate中声明的元素时遇到了一个问题,之后该元素像ContentTemplate一样应用于TabItem对象。 我看到在这个问题上已经有了一些解决方案,但在我的情况下,没有一个真正起作用,我想了解为什么(显然我在某些地方犯了错误) 下面是一个示例代码:

    <DataTemplate x:Key="TabItemDataTemplate">             
        <Grid HorizontalAlignment="Stretch" 
            VerticalAlignment="Stretch" Name="templateGrid">
            <Grid.RowDefinitions>
                <RowDefinition Height="6.0*"> </RowDefinition>
                <RowDefinition Height="6" ></RowDefinition>
                <RowDefinition Height="6.0*" ></RowDefinition>
                <RowDefinition Height="*" ></RowDefinition>
            </Grid.RowDefinitions>                
    
            <ListView x:Name="repoView" Grid.Row="0" 
                VerticalAlignment="Stretch"
                ItemsSource="{Binding Source={StaticResource  DataProviderForListView}}">                        
                <GridView>
                    <GridViewColumn Header="State"
                        DisplayMemberBinding="{Binding Path=RepositoryItemState}"/>
                    <GridViewColumn Header="Working Copy Rev num."
                        DisplayMemberBinding="{Binding Path=WCRevision}"/>
                    <GridViewColumn Header="Repository Rev num."
                        DisplayMemberBinding="{Binding Path=RepoRevision}"/>
                    <GridViewColumn Header="User"
                        DisplayMemberBinding="{Binding Path=Account}"/>
                    <GridViewColumn Header="Item"
                        DisplayMemberBinding="{Binding Path=ItemName}"/>
                </GridView>
            </ListView>
    
            <GridSplitter x:Name="gridSplitter" Grid.Row="1"
                ResizeDirection="Rows" Background="Gray" 
                Height="4" HorizontalAlignment="Stretch"
                Style="{StaticResource gridSplitterStyle}"/>
    
            <RichTextBox x:Name="rowView" Grid.Row="2" 
                BorderBrush="Bisque" VerticalAlignment="Stretch"
                IsReadOnly="True" Background="YellowGreen"
                FontFamily="Comic Sans Serif"/>
    
    
            <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" />
            <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" />
            <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}"  />
        </Grid>
    </DataTemplate>
    

    此代码按语法按以下方式应用于给定的TabItem对象:

    this.ContentTemplate = FindResource("TabItemDataTemplate") as DataTemplate;
    

    在我需要访问DataTemplate中声明的ListView元素之后,我执行在internet上以及在这个站点上找到的代码。下面是一个简短的例子:

    /* Getting the ContentPresenter of myListBoxItem*/          
    ContentPresenter myContentPresenter =
        FindVisualChild<ContentPresenter>(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);
    

    问题1:在这种情况下 ContentPresenter的值为Null,因此代码执行崩溃。 问题2:好的,我想,可能我需要直接导航项目内容,所以代码或多或少会变成:

    /* Getting the ContentPresenter of myListBoxItem*/          
    ContentPresenter myContentPresenter =
        FindVisualChild<ContentPresenter>(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);
    

    是TabItem对象。但是strage的东西,它的内容模板 与上面指定的完全不同。我肯定我遗漏了什么,你能帮我解决这个问题吗? 非常感谢。

    3 回复  |  直到 15 年前
        1
  •  0
  •   Andy    15 年前

    您不想使用的任何模板属性 TabItem ListView 直接,而不是通过 DataTemplate .

        2
  •  0
  •   Tigran    15 年前

    好了,我们来了:) 如上所述,我使用了LoadContent方法,它返回ListView对象,但顺便说一句,UI实际使用的不是ListView。因此,为了解决这个问题,我添加了静态属性来保存我的真实ListView对象(静态,因为我有一个DataTemplate,其中包含多个TabItems共享的ListView,因此ListView也共享),并将事件处理程序添加到我的DataTemplate-> 加载 . 捕捉到这个事件,在我的例子中,只会在应用程序的生命周期中引发一个事件 RoutedEvent's 原始来源 希望我的解决方案能帮助别人。

        3
  •  0
  •   Adi Lester    12 年前

    简单地说,如果你有 DataGrid TemplateColumn 其中包含数据模板,您可以使用以下代码示例:

    <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));
    
    • 哪里 photo 是文本块的名称
    • 哪里 photoPathColumn 数据网格 模板柱 .