【问题标题】:Insert textblock into treeviewitem将文本块插入树视图项
【发布时间】:2020-01-19 10:47:15
【问题描述】:

我将文本块添加到 treeviewitem。 但 treeviewitem.header 无法绑定到此文本块。 我该怎么办?我想在树视图上添加对象。想要开发一个自定义树视图。

<ControlTemplate TargetType="{x:Type TreeViewItem}">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition MinWidth="19" Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <ToggleButton x:Name="Expander" ClickMode="Press" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ExpandCollapseToggleStyle}"/>
    <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="1" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
        <ContentPresenter x:Name="PART_Header" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
    </Border>
    <ItemsPresenter x:Name="ItemsHost" Grid.ColumnSpan="2" Grid.Column="1" Grid.Row="1"/>
</Grid>

TO

<ControlTemplate TargetType="{x:Type TreeViewItem}">
<Grid Background="#15232e">
    <Grid.ColumnDefinitions>
        <ColumnDefinition MinWidth="19" Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <ToggleButton x:Name="Expander" ClickMode="Press" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ExpandCollapseToggleStyle}"/>
    <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="Transparent" Grid.Column="1" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
        <TextBlock x:Name="PART_Header" Text="{Binding ??????}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
    </Border>
    <ItemsPresenter x:Name="ItemsHost" Grid.ColumnSpan="2" Grid.Column="1" Grid.Row="1"/>
</Grid>

【问题讨论】:

  • 如果Header 设置为stringText="{TemplateBinding Header}" 应该可以工作。但是为什么要用TextBlock 替换ContentPresenter
  • 无论如何,您都可以将控件放在标题中。这不会满足你的要求吗? docs.microsoft.com/en-us/dotnet/framework/wpf/controls/…
  • @SerkanUlusoy:您尝试过我使用{TemplateBinding} 的建议了吗?

标签: c# wpf blend


【解决方案1】:

可能还有其他方法可以解决您的问题,但要直接解决您的问题,ContentPresenter 正在获取整个对象。

如果你的视图模型看起来像这样:

public class Data 
{
   public string Name { get; set; }
   public List<Data> Children { get; set; }
}

那么基本上ContentPresenterContentSource属性基本上就是要设置Content、ContentTemplate等。 (见https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.contentpresenter.contentsource?view=netframework-4.8

在您的树视图中,分层数据模板可能如下所示:

<TreeView.ItemTemplate>
   <HierarchicalDataTemplate ItemsSource="{Binding Children}">
      <TextBlock Text="{Binding Name}" />
   </HierarchicalDataTemplate>
</TreeView.ItemTemplate>

里面的部分基本上是给presenter的数据模板,ItemsSource是如何确定children的。

所以 - 对于您的情况,您要么想要将 Text 直接绑定到 HierarchicalDataTemplate 中的数据,如上所述,或者如果您确实想要自定义 TreeViewItem 的 ControlTemplate,您可以将 TextBlock 提供为PART_Header,但绑定到树的元素。鉴于上面的 Data 类,它看起来像这样:

<ControlTemplate>
   ...
   <TextBlock x:Name="PART_Header" Text="{Binding Name}"/>
   ...
</ControlTemplate>

如果这符合您的用例,请告诉我,不幸的是,如果没有更多信息,很难说。祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 1970-01-01
    相关资源
    最近更新 更多