【问题标题】:How to bind a tab control header to to a tab control content?如何将选项卡控件标题绑定到选项卡控件内容?
【发布时间】:2015-09-29 07:44:28
【问题描述】:

我有一个标签控件,其中定义了(如 TabControl.ItemContainerStyle 中的 Style)模板 HeaderTemplateContentTemplate .选项卡控件没有项目源。项目内容有一个自定义用户控件,其属性为 Status。项目标题有一个文本块,必须在其中显示此状态。 我的问题是如何绑定这个?

<TabControl x:Name="tbMain">
    <TabControl.ItemContainerStyle>
        <Style TargetType="TabItem">
            <Setter Property="Padding" Value="0"/>
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <local:Session x:Name="session"/>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="HeaderTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <Image VerticalAlignment="Center" Width="18" Height="18" Source="Resources/bullet_green.png"/>
                            <TextBlock Grid.Column="1" VerticalAlignment="Center" Margin="1" Foreground="Green" Text="{Binding ?? session.Status}"/>
                            <Button x:Name="btnDelete" Grid.Column="2" Margin="1" Content="asdasd" VerticalAlignment="Center" Style="{DynamicResource CloseButton}" Click="btnDelete_Click" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Name}"/>
                        </Grid>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TabControl.ItemContainerStyle>
</TabControl>

【问题讨论】:

  • 请发布您的(相关)XAML 标记。
  • 您是否尝试使用 'ElementName' 语法进行绑定?类似 ''
  • @Bruno 是的,我试过了。它对我不起作用。我认为问题在于,内容模板的元素 session 和标题模板的文本块位于不同的范围内,彼此不可见。这正是问题所在,如何从该文本块访问 session 元素。我认为,它应该类似于 -> 父控件(这是单个选项卡)-> 内容 -> 会话,但我不知道如何实现。

标签: wpf tabcontrol


【解决方案1】:

我猜您的自定义控件正在从视图模型或其他东西接收其数据 (Status)。然后,与其尝试从一个元素绑定到另一个元素,也许您可​​以将两个 TextBlock 控件绑定到同一个 VM。

以下示例适用于我:

using System.Windows;
using System.Windows.Controls;

namespace StackOverflow
{

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        Status = "STATUS FROM VM";
    }

    public string Status { get; set; }
}

public class Session : Control
{
    public static readonly DependencyProperty StatusProperty =
        DependencyProperty.Register("Status", typeof(string), typeof(Session), new PropertyMetadata("My Status"));

    static Session()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(Session), new FrameworkPropertyMetadata(typeof(Session)));
    }

    public string Status
    {
        get { return (string)GetValue(StatusProperty); }
        set { SetValue(StatusProperty, value); }
    }
}
}

还有 XAML:

<TabControl x:Name="tbMain">
        <TabControl.ItemContainerStyle>
            <Style TargetType="TabItem">
                <Setter Property="Padding" Value="0"/>
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <stackOverflow:Session x:Name="session" Status="{Binding Status}">
                                <stackOverflow:Session.Template>
                                    <ControlTemplate TargetType="stackOverflow:Session">
                                        <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=TabControl}, Path=DataContext.Status}"></TextBlock>
                                    </ControlTemplate>
                                </stackOverflow:Session.Template>
                            </stackOverflow:Session>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>

                                <TextBlock Grid.Column="1" VerticalAlignment="Center" Margin="1" Foreground="Green" Text="{Binding RelativeSource={RelativeSource AncestorType=TabControl}, Path=DataContext.Status}"/>
                                <Button x:Name="btnDelete" Grid.Column="2" Margin="1" Content="asdasd" VerticalAlignment="Center"  />
                            </Grid>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TabControl.ItemContainerStyle>
        <TabItem></TabItem>
    </TabControl>

【讨论】:

  • 非常感谢您的解决方案。我喜欢它,但它与我需要的有点不同。在可能的情况下 session 控件有一个套接字客户端,我需要在选项卡的标题中显示一个套接字连接状态。我需要避免在主窗口类中存储状态。如果我使用这个解决方案,我可能只是为选项卡控件定义一个 Items Source 并绑定它。但是主窗口类不应该有任何会话信息。 Session 只有一个 Status 属性,它必须显示在标题中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-10
  • 2018-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多