如何将 TabItem 的 Header 与 header 的 TabItem DataTemplate 中的文本绑定?
How can I bind the Header of an TabItem with a text in my TabItem DataTemplate for the header?
我在 App.xaml 文件中为我的 TabItem 创建了这个模板,这样我就可以为我的 TabItem 的 Headers.
使用自定义样式
<Style x:Key="MyTabItemHeader" TargetType="{x:Type TabItem}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate DataType="{x:Type TabItem}">
<Border BorderBrush="Gray" CornerRadius="6" Background="LightGray" BorderThickness="0.5" Padding="1" Margin="2,4,2,4">
<StackPanel Orientation="Horizontal" Height="28" MinWidth="60" MinHeight="20" >
<fa:FontAwesome Name="ItemIconXaml" FontSize="22" Margin="10,0" Foreground="#0C0239" VerticalAlignment="Center"/>
<TextBlock Text="{Binding Header}" Name="ItemNamexaml" FontSize="20" VerticalAlignment="Center" Foreground="#0C0239" FontFamily="Segoe Script"/>
<fa:FontAwesome Icon="Close" FontSize="18" Margin="10,0" VerticalAlignment="Center" Foreground="#CCA09F9F" HorizontalAlignment="Right" MouseDown="FontAwesome_MouseDown"/>
</StackPanel>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
在 window 的代码中,它负责显示带有 tabItems 的 TabControl。所以我将 TabItems 动态添加到 TabControl。
public partial class Accueil : Window
{
public Accueil()
{
InitializeComponent();
// Definition des contents des Tabitems
AjouterPatientTabItem.Content = ajouterPatientUserControl;
FacturesDuJourTabItem.Content = facturesDuJourUserControl;
MainHomeTabItem.Content = mainHomeUserControl;
RegistreTabItem.Content = registreUserControl;
ParamètresTabItem.Content = paramètresUserControl;
// Attribution des names des TabItem
//Ajouter Patient TabItem
AjouterPatientTabItem.Header = "Nouveau patient";
// AjouterPatientTabItem.Style = (Style)Application.Current.Resources["MyTabItemHeader"];
//Facture du patient TabItem
FacturesDuJourTabItem.Header = "Factures du jour";
// FacturesDuJourTabItem.Style = (Style)Application.Current.Resources["MyTabItemHeader"];
//MainHome
MainHomeTabItem.Header = "Accueil";
// MainHomeTabItem.Style = (Style)Application.Current.Resources["MyTabItemHeader"];
//Registre
RegistreTabItem.Header = "Registre";
// RegistreTabItem.Style = (Style)Application.Current.Resources["MyTabItemHeader"];
//Paramêtres
ParamètresTabItem.Header = "Paramètres";
// ParamètresTabItem.Style = (Style)Application.Current.Resources["MyTabItemHeader"];
//
// TabControlItems.ItemContainerStyle = (Style)Application.Current.Resources["TabItemStyle"];
TabControlItems.ItemContainerStyle = (Style)Application.Current.Resources["MyTabItemHeader"];
displayedItems.Add(MainHomeTabItem);
this.TabControlItems.ItemsSource = displayedItems;
}
private ObservableCollection<TabItem> displayedItems = new ObservableCollection<TabItem>();
public ObservableCollection<TabItem> DisplayedItems
{
get { return displayedItems; }
set { displayedItems = value; }
}
//Instanciation des Usercontrols
public Home ajouterPatientUserControl = new Home("Accueil", "Home");
public FacturesDuJour facturesDuJourUserControl = new FacturesDuJour();
public mainHome mainHomeUserControl = new mainHome();
public Paramètres paramètresUserControl = new Paramètres();
public Registre registreUserControl = new Registre();
// Instanciation des Tabs items
public TabItem AjouterPatientTabItem = new TabItem();
public TabItem FacturesDuJourTabItem = new TabItem();
public TabItem MainHomeTabItem = new TabItem();
public TabItem ParamètresTabItem = new TabItem();
public TabItem RegistreTabItem = new TabItem();
private void AccueilBtn_Click(object sender, RoutedEventArgs e)
{
// MainHomeTabItem.IsSelected = true;
if (!displayedItems.Contains(MainHomeTabItem))
{
displayedItems.Add(MainHomeTabItem);
TabControlItems.SelectedItem = MainHomeTabItem;
}
else
{
TabControlItems.SelectedItem = MainHomeTabItem;
}
}
private void FactureDuJourBtn_Click(object sender, RoutedEventArgs e )
{
// FactureDuJourTabItem.IsSelected = true;
if (!displayedItems.Contains(FacturesDuJourTabItem))
{
displayedItems.Add(FacturesDuJourTabItem);
TabControlItems.SelectedItem = FacturesDuJourTabItem;
}
else
{
TabControlItems.SelectedItem = FacturesDuJourTabItem;
}
}
private void QuitterBtn_Click(object sender, RoutedEventArgs e)
{
Window Quitter = new QuitterMessageBox();
Quitter.ShowDialog();
}
private void AjouterNouveauPatientBtn_Click(object sender, RoutedEventArgs e)
{
// AccueilTabItem.IsSelected = true;
if (!displayedItems.Contains(item: AjouterPatientTabItem))
{
displayedItems.Add(AjouterPatientTabItem);
TabControlItems.SelectedItem = AjouterPatientTabItem;
}
else
{
TabControlItems.SelectedItem = AjouterPatientTabItem;
}
}
private void RegistreBtn_Click(object sender, RoutedEventArgs e)
{
// RegistreTabItem.IsSelected = true;
if (!displayedItems.Contains(item: RegistreTabItem))
{
displayedItems.Add(RegistreTabItem);
TabControlItems.SelectedItem = RegistreTabItem;
}
else
{
TabControlItems.SelectedItem = RegistreTabItem;
}
}
private void parametresBtn_Click(object sender, RoutedEventArgs e)
{
// ParamètresTabItem.IsSelected = true;
if (!displayedItems.Contains(item: ParamètresTabItem))
{
displayedItems.Add(ParamètresTabItem);
TabControlItems.SelectedItem = ParamètresTabItem;
}
else
{
TabControlItems.SelectedItem = ParamètresTabItem;
}
}
private void Window_closing(object sender, CancelEventArgs e)
{
e.Cancel = true;
Window Quitter = new QuitterMessageBox();
Quitter.ShowDialog();
}
private void AccueilTabCloseTabBtn_MouseDown(object sender, MouseButtonEventArgs e)
{
displayedItems.RemoveAt(TabControlItems.SelectedIndex);
}
}
在 Accueil Xaml 中 class 我创建了一个 tabControl
<TabControl x:Name="TabControlItems" ItemsSource="{Binding displayedItems}">
</TabControl>
当我 运行 应用程序时,我不显示 header 文本。它什么也没显示。
请帮助我。
对不起,如果我的英语不完美。
目前 Binding
到 TabControl.ItemsSource
没有解决。这是因为在当前TabControl.DataContext
.
中找不到属性displayedItems
将this.DataContext = this;
添加到Accueil
的构造函数中:
public Accueil()
{
InitializeComponent();
this.DataContext = this;
}
这样 Accueil
将隐式分配给(继承)TabControl.DataContext
。
或者,例如,如果您不想将父控件及其子控件的 DataContext
设置为 Accueil
,请将 Binding
更改为
<TabControl ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=Accueil}, Path=displayedItems}" />
TabControl
不应绑定到 TabItem
的集合(它们是物品容器),而应绑定到模型 类 的集合。 TabControl
将在内部生成这些容器,自动应用样式和模板。
您应该绑定到 DataContext
本身,而不是尝试绑定到 Header
属性:
<TextBlock Text="{Binding}" Name="ItemNamexaml"
FontSize="20" VerticalAlignment="Center" Foreground="#0C0239" FontFamily="Segoe Script"/>
HeaderTemplate
中根元素的DataContext
是Header
属性中的值。
我在 App.xaml 文件中为我的 TabItem 创建了这个模板,这样我就可以为我的 TabItem 的 Headers.
使用自定义样式<Style x:Key="MyTabItemHeader" TargetType="{x:Type TabItem}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate DataType="{x:Type TabItem}">
<Border BorderBrush="Gray" CornerRadius="6" Background="LightGray" BorderThickness="0.5" Padding="1" Margin="2,4,2,4">
<StackPanel Orientation="Horizontal" Height="28" MinWidth="60" MinHeight="20" >
<fa:FontAwesome Name="ItemIconXaml" FontSize="22" Margin="10,0" Foreground="#0C0239" VerticalAlignment="Center"/>
<TextBlock Text="{Binding Header}" Name="ItemNamexaml" FontSize="20" VerticalAlignment="Center" Foreground="#0C0239" FontFamily="Segoe Script"/>
<fa:FontAwesome Icon="Close" FontSize="18" Margin="10,0" VerticalAlignment="Center" Foreground="#CCA09F9F" HorizontalAlignment="Right" MouseDown="FontAwesome_MouseDown"/>
</StackPanel>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
在 window 的代码中,它负责显示带有 tabItems 的 TabControl。所以我将 TabItems 动态添加到 TabControl。
public partial class Accueil : Window
{
public Accueil()
{
InitializeComponent();
// Definition des contents des Tabitems
AjouterPatientTabItem.Content = ajouterPatientUserControl;
FacturesDuJourTabItem.Content = facturesDuJourUserControl;
MainHomeTabItem.Content = mainHomeUserControl;
RegistreTabItem.Content = registreUserControl;
ParamètresTabItem.Content = paramètresUserControl;
// Attribution des names des TabItem
//Ajouter Patient TabItem
AjouterPatientTabItem.Header = "Nouveau patient";
// AjouterPatientTabItem.Style = (Style)Application.Current.Resources["MyTabItemHeader"];
//Facture du patient TabItem
FacturesDuJourTabItem.Header = "Factures du jour";
// FacturesDuJourTabItem.Style = (Style)Application.Current.Resources["MyTabItemHeader"];
//MainHome
MainHomeTabItem.Header = "Accueil";
// MainHomeTabItem.Style = (Style)Application.Current.Resources["MyTabItemHeader"];
//Registre
RegistreTabItem.Header = "Registre";
// RegistreTabItem.Style = (Style)Application.Current.Resources["MyTabItemHeader"];
//Paramêtres
ParamètresTabItem.Header = "Paramètres";
// ParamètresTabItem.Style = (Style)Application.Current.Resources["MyTabItemHeader"];
//
// TabControlItems.ItemContainerStyle = (Style)Application.Current.Resources["TabItemStyle"];
TabControlItems.ItemContainerStyle = (Style)Application.Current.Resources["MyTabItemHeader"];
displayedItems.Add(MainHomeTabItem);
this.TabControlItems.ItemsSource = displayedItems;
}
private ObservableCollection<TabItem> displayedItems = new ObservableCollection<TabItem>();
public ObservableCollection<TabItem> DisplayedItems
{
get { return displayedItems; }
set { displayedItems = value; }
}
//Instanciation des Usercontrols
public Home ajouterPatientUserControl = new Home("Accueil", "Home");
public FacturesDuJour facturesDuJourUserControl = new FacturesDuJour();
public mainHome mainHomeUserControl = new mainHome();
public Paramètres paramètresUserControl = new Paramètres();
public Registre registreUserControl = new Registre();
// Instanciation des Tabs items
public TabItem AjouterPatientTabItem = new TabItem();
public TabItem FacturesDuJourTabItem = new TabItem();
public TabItem MainHomeTabItem = new TabItem();
public TabItem ParamètresTabItem = new TabItem();
public TabItem RegistreTabItem = new TabItem();
private void AccueilBtn_Click(object sender, RoutedEventArgs e)
{
// MainHomeTabItem.IsSelected = true;
if (!displayedItems.Contains(MainHomeTabItem))
{
displayedItems.Add(MainHomeTabItem);
TabControlItems.SelectedItem = MainHomeTabItem;
}
else
{
TabControlItems.SelectedItem = MainHomeTabItem;
}
}
private void FactureDuJourBtn_Click(object sender, RoutedEventArgs e )
{
// FactureDuJourTabItem.IsSelected = true;
if (!displayedItems.Contains(FacturesDuJourTabItem))
{
displayedItems.Add(FacturesDuJourTabItem);
TabControlItems.SelectedItem = FacturesDuJourTabItem;
}
else
{
TabControlItems.SelectedItem = FacturesDuJourTabItem;
}
}
private void QuitterBtn_Click(object sender, RoutedEventArgs e)
{
Window Quitter = new QuitterMessageBox();
Quitter.ShowDialog();
}
private void AjouterNouveauPatientBtn_Click(object sender, RoutedEventArgs e)
{
// AccueilTabItem.IsSelected = true;
if (!displayedItems.Contains(item: AjouterPatientTabItem))
{
displayedItems.Add(AjouterPatientTabItem);
TabControlItems.SelectedItem = AjouterPatientTabItem;
}
else
{
TabControlItems.SelectedItem = AjouterPatientTabItem;
}
}
private void RegistreBtn_Click(object sender, RoutedEventArgs e)
{
// RegistreTabItem.IsSelected = true;
if (!displayedItems.Contains(item: RegistreTabItem))
{
displayedItems.Add(RegistreTabItem);
TabControlItems.SelectedItem = RegistreTabItem;
}
else
{
TabControlItems.SelectedItem = RegistreTabItem;
}
}
private void parametresBtn_Click(object sender, RoutedEventArgs e)
{
// ParamètresTabItem.IsSelected = true;
if (!displayedItems.Contains(item: ParamètresTabItem))
{
displayedItems.Add(ParamètresTabItem);
TabControlItems.SelectedItem = ParamètresTabItem;
}
else
{
TabControlItems.SelectedItem = ParamètresTabItem;
}
}
private void Window_closing(object sender, CancelEventArgs e)
{
e.Cancel = true;
Window Quitter = new QuitterMessageBox();
Quitter.ShowDialog();
}
private void AccueilTabCloseTabBtn_MouseDown(object sender, MouseButtonEventArgs e)
{
displayedItems.RemoveAt(TabControlItems.SelectedIndex);
}
}
在 Accueil Xaml 中 class 我创建了一个 tabControl
<TabControl x:Name="TabControlItems" ItemsSource="{Binding displayedItems}">
</TabControl>
当我 运行 应用程序时,我不显示 header 文本。它什么也没显示。 请帮助我。
对不起,如果我的英语不完美。
目前 Binding
到 TabControl.ItemsSource
没有解决。这是因为在当前TabControl.DataContext
.
displayedItems
将this.DataContext = this;
添加到Accueil
的构造函数中:
public Accueil()
{
InitializeComponent();
this.DataContext = this;
}
这样 Accueil
将隐式分配给(继承)TabControl.DataContext
。
或者,例如,如果您不想将父控件及其子控件的 DataContext
设置为 Accueil
,请将 Binding
更改为
<TabControl ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=Accueil}, Path=displayedItems}" />
TabControl
不应绑定到 TabItem
的集合(它们是物品容器),而应绑定到模型 类 的集合。 TabControl
将在内部生成这些容器,自动应用样式和模板。
您应该绑定到 DataContext
本身,而不是尝试绑定到 Header
属性:
<TextBlock Text="{Binding}" Name="ItemNamexaml"
FontSize="20" VerticalAlignment="Center" Foreground="#0C0239" FontFamily="Segoe Script"/>
HeaderTemplate
中根元素的DataContext
是Header
属性中的值。