如何进行Wpf TabItem样式HeaderTemplate绑定?

发布于 2024-12-05 17:06:39 字数 778 浏览 1 评论 0原文

如何进行Wpf TabItem样式HeaderTemplate绑定?

代码:

<TabControl x:Name="tabCtrlMain" ItemsSource="{Binding Items}" >
        <TabControl.Resources>
            <Style TargetType="TabItem">
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate DataType="{x:Type TabItem}">
                            <TextBlock Text="{Binding FileName}"/>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TabControl.Resources>
    </TabControl>

绑定时此代码不起作用:

<TextBlock Text="{Binding FileName}"/>

How To do Wpf TabItem Style HeaderTemplate Binding?

Code:

<TabControl x:Name="tabCtrlMain" ItemsSource="{Binding Items}" >
        <TabControl.Resources>
            <Style TargetType="TabItem">
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate DataType="{x:Type TabItem}">
                            <TextBlock Text="{Binding FileName}"/>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TabControl.Resources>
    </TabControl>

this code is not working when binding:

<TextBlock Text="{Binding FileName}"/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我只土不豪 2024-12-12 17:06:39

我知道这现在已经非常旧了,但我想我应该为了完整性和历史准确性而投入两分钱:)

我更喜欢使用 ItemContainerStyle 来做同样的事情,只是因为它对我来说感觉更干净一些因为它准确地说明了目的:

<TabControl ItemsSource="{Binding Items}">
  <TabControl.ItemContainerStyle>
    <Style TargetType="TabItem">
      <Setter Property="Header" Value="{Binding FileName}" />
      <Setter Property="HeaderTemplate">
        <Setter.Value>
          <DataTemplate>
            <Border>
              <TextBlock Text="{Binding Content}" />
            </Border>
          </DataTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </TabControl.ItemContainerStyle>
</TabControl>

此外,如果唯一的目标是将文件名放入选项卡中,那么它可以简单得多:

<TabControl ItemsSource="{Binding Items}" DisplayMemberPath="FileName" />

I know this is awfully old now, but I thought I'd throw my two cents in just for the sake of completeness and historical accuracy :)

I prefer to use the ItemContainerStyle to do the same thing just because it feels a little cleaner to me because it states the purpose exactly:

<TabControl ItemsSource="{Binding Items}">
  <TabControl.ItemContainerStyle>
    <Style TargetType="TabItem">
      <Setter Property="Header" Value="{Binding FileName}" />
      <Setter Property="HeaderTemplate">
        <Setter.Value>
          <DataTemplate>
            <Border>
              <TextBlock Text="{Binding Content}" />
            </Border>
          </DataTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </TabControl.ItemContainerStyle>
</TabControl>

Also, if the only goal is to get the FileName into the tabs then it can be much simpler:

<TabControl ItemsSource="{Binding Items}" DisplayMemberPath="FileName" />
会发光的星星闪亮亮i 2024-12-12 17:06:39

试试这个相反,

<TabControl x:Name="tabCtrlMain" ItemsSource="{Binding Items}" >
        <TabControl.Resources>
            <Style TargetType="TabItem">
                <Setter Property="Header" Value="{Binding FileName}" />
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate DataType="{x:Type TabItem}">
                            <Border x:Name="grid">
                                    <ContentPresenter>
                                        <ContentPresenter.Content>
                                            <TextBlock Text="{TemplateBinding Content}"/>
                                        </ContentPresenter.Content>
                                    </ContentPresenter>
                                </Border>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TabControl.Resources>
    </TabControl>

Try this Instead,

<TabControl x:Name="tabCtrlMain" ItemsSource="{Binding Items}" >
        <TabControl.Resources>
            <Style TargetType="TabItem">
                <Setter Property="Header" Value="{Binding FileName}" />
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate DataType="{x:Type TabItem}">
                            <Border x:Name="grid">
                                    <ContentPresenter>
                                        <ContentPresenter.Content>
                                            <TextBlock Text="{TemplateBinding Content}"/>
                                        </ContentPresenter.Content>
                                    </ContentPresenter>
                                </Border>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TabControl.Resources>
    </TabControl>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文