从动态填充的tabcontrol中获取TabItem标头

时间:2016-03-02 15:27:42

标签: wpf xaml

我有一个tabservtrol,它是从一个observablecollection中填充的。基于observablecollection,tabitems标头被设置并且tabitem中的datagrid被填充。我想要做的是获取tabitem标题并将其设置在文本块中。我能够获取tabcontrol名称并将其设置为文本块文本,但不是所选tabitem中的标题。

<TabControl  Grid.Row="1" ItemsSource="{Binding Workspaces}" Height="Auto" Background="Transparent" x:Name="TabsName" >
                    <TabControl.Resources>
                        <localHelper:HeaderAppendConverter x:Key="HeaderAppedConvrter"/>
                    </TabControl.Resources>
                    <TabControl.ItemTemplate >
                        <DataTemplate >
                            <TextBlock  Text="{Binding HeaderText}"  />
                        </DataTemplate>
                    </TabControl.ItemTemplate>

                    <TabControl.ContentTemplate>
                        <DataTemplate x:Name="Tabsitems">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="50"/>
                                    <RowDefinition Height="725" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="1600" />
                                </Grid.ColumnDefinitions>
                                 <dxg:GridControl Grid.Row="1" x:Name="NameGrid" ItemsSource="{Binding Data}" >
                                    <dxgcore:GridControl.Columns>

                                        <dxg:GridColumn Name="Month1" FieldName="Month01"  Visible="True" AllowEditing="False" HorizontalHeaderContentAlignment="Center" CellStyle="{StaticResource NumberCellStyle}">
                                            <dxg:GridColumn.EditSettings>
                                                <dxe:TextEditSettings HorizontalContentAlignment="Right" />
                                            </dxg:GridColumn.EditSettings>
                                            <dxg:GridColumn.Header>
                                                <TextBlock  Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TabControl}}, Path=SelectedItem.Header, Converter={StaticResource HeaderAppendConverter}, ConverterParameter='01'}" />
                                            </dxg:GridColumn.Header>

                                        </dxg:GridColumn>
                                          </dxg:GridColumn>
                                    </dxgcore:GridControl.Columns>
                                    <dxgcore:GridControl.View>
                                        <dxgcore:TableView x:Name="NameGridView" 
                                       AllowEditing="False" 
                                       AllowBestFit="True" 
                                       AllowMoving="True" 
                                       AllowColumnFiltering="True"
                                       IsColumnMenuEnabled="True"
                                       ShowGroupPanel="False"
                                       ShowAutoFilterRow="True"
                                       AutoWidth="False" 
                                       NavigationStyle="Cell"
                                       VerticalScrollbarVisibility="Visible"
                                       HorizontalScrollbarVisibility="Visible"
                                       RowStyle="{StaticResource customRowStyle}" >
                                        </dxgcore:TableView>
                                    </dxgcore:GridControl.View>
                                </dxg:GridControl>

                            </Grid>
                        </DataTemplate>
                    </TabControl.ContentTemplate>
                </TabControl>


public class WorkSpace : INotifyPropertyChanged
{

    private string headerText;
    public string HeaderText { get { return headerText; } set { headerText = value; OnPropertyChanged("HeaderText"); } }

    public override string ToString()
    {
        return HeaderText;
    }

    private List<Data> data;
    public List<Data> Data { get { return data; } set { data = value; OnPropertyChanged("Data"); } }

这一行

<TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TabControl}}, Path=Name}" />

将tabcontrol名称设置为文本块如何更改它以获取所选的tabitem标题文本

1 个答案:

答案 0 :(得分:0)

将流动的转换器添加到您的项目中:

public class HeaderAppendConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return string.Format("{0}{1}",value, parameter);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Binding.DoNothing;
    }
}

然后将StaticResource:

添加到TabControl容器资源中
  <local:HeaderAppendConverter x:Key="HeaderAppedConvrter"/>

最后更新TextBlock;

<TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TabControl}}, Path=SelectedItem.Header, Converter={StaticResource HeaderAppendConverter}, ConverterParameter=columnSuffix}" />
您可以在ConverterParameter中传递自定义字符串

完整的解决方案如下:

解决方案结构是: Solution Struct

主窗口XAML代码:

<Window x:Class="Twest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Twest"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <ResourceDictionary>
        <local:HeaderAppendConverter x:Key="HeaderAppendConverter" />
    </ResourceDictionary>
</Window.Resources>
<Grid>
    <TabControl  Grid.Row="1"  Height="Auto" Background="Transparent" x:Name="TabControlMainName">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <TextBlock  Text="{Binding HeaderText}" />
            </DataTemplate>
        </TabControl.ItemTemplate>

        <TabControl.ContentTemplate>
            <DataTemplate x:Name="Tabsitems">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="50" />
                        <RowDefinition Height="725" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="1600" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TabControl}}, Path=SelectedItem.Header, Converter={StaticResource HeaderAppendConverter}, ConverterParameter= columnSuffix}" />
                </Grid>
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>
</Grid>

MainWindow代码背后:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace Twest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
    public MainWindow()
    {
        InitializeComponent();
        Loaded += MainWindow_Loaded;
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        List<TabItem> tabs = new List<TabItem>();
        tabs.Add(new TabItem() { Name = "tab1", Header = "TAB 01" });
        tabs.Add(new TabItem() { Name = "tab2", Header = "TAB 02" });
        TabControlMainName.ItemsSource = tabs;
    }
}

}