我有一个定义的标签控件( TabControl.ItemContainerStyle 中的样式)模板 HeaderTemplate 和 ContentTemplate 。选项卡控件没有项目源。项目内容具有自定义用户控件,其属性为状态。 Item Header有一个文本块,必须显示此状态。 我的问题是如何绑定它?
<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>
答案 0 :(得分: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>