简体   繁体   中英

Assigning datacontext to autogenerated tabitem in tabcontrol

I am trying to assign an object to datacontext from TabItem. To get an idea, look at the following code sample

<UserControl x:Class="CustomCopyNas.UserControls.LoginUsers"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:igWindows="http://infragistics.com/Windows"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">

    <Grid Margin="5,0,5,0">
        <igWindows:XamTabControl Name="_xamTabControl"
             TabLayoutStyle="MultiRowSizeToFit"
             MaximumTabRows="4"
             MaximumSizeToFitAdjustment="50"
             MinimumTabExtent="100"
             InterTabSpacing="2"
             InterRowSpacing="2"
             Theme="Metro" 
             AllowTabClosing="False"
             TabItemCloseButtonVisibility="WhenSelectedOrHotTracked">
            <igWindows:XamTabControl.ContentTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Prop}"/>
                </DataTemplate>
            </igWindows:XamTabControl.ContentTemplate>
        </igWindows:XamTabControl>
    </Grid>
</UserControl>

As you can see, I use datatemplate for TabItem content appearance, TextBox. The TextBox Text property is binding to a property from the datacontext.

And the partial class from UserControl

public class Foo
{
    public string Prop {
        get { return "Hello Foo"; }
    }
}

/// <summary>
/// Interaction logic for LoginUsers.xaml
/// </summary>
public partial class LoginUsers : UserControl
{
    public LoginViewModel LoginViewModel = new LoginViewModel("file.xml");

    public LoginUsers()
    {
        InitializeComponent();

        foreach (var server in LoginViewModel.ServerUsers)
        {
            string header = server.Server;
            string name = "tabItem" + header;
            _xamTabControl.Items.Add(new TabItemEx() { Header = header, Name = name, DataContext = new Foo() });
        }
    }
}

As output on TabItem content I've got nothing, so emtpy content, why?

You don't seem to have declared your TabControl XAML properly. It is customary to see it defined more like this, using the TabControl.ItemsSource property:

<TabControl ItemsSource="{Binding YourCollectionProperty}">
    <TabControl.ItemTemplate> <!-- Header Template-->
        <DataTemplate>
            <TextBlock Text="{Binding HeaderText}" />
        </DataTemplate>
    </TabControl.ItemTemplate>
    <TabControl.ContentTemplate> <!-- Body Template-->
        <DataTemplate>
            <TextBlock Text="{Binding BodyText}" />
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

For this to work, you'll need to create a custom class that has HeaderText and BodyText properties in it. Then you'll need to create a public ObservableCollection<YourCustomClass> collection property in your code behind named YourCollectionProperty .

Please note that the Binding s inside the two DataTemplate s will automatically have their DataContext s set to an item from the YourCollectionProperty collection and that is why your Binding to the Prop property didn't work.

Try moving the foreach loop so that it fires when the _xamTabControl.Loaded event fires. That should do the trick

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM