简体   繁体   English

在tabcontrol中将数据上下文分配给自动生成的Tabitem

[英]Assigning datacontext to autogenerated tabitem in tabcontrol

I am trying to assign an object to datacontext from TabItem. 我正在尝试从TabItem将对象分配给datacontext。 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. 如您所见,我将datatemplate用于TabItem内容外观TextBox。 The TextBox Text property is binding to a property from the datacontext. TextBox Text属性绑定到datacontext中的属性。

And the partial class from UserControl 还有来自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? 作为TabItem内容的输出,我什么也没有,所以没有内容,为什么?

You don't seem to have declared your TabControl XAML properly. 您似乎没有正确声明TabControl XAML。 It is customary to see it defined more like this, using the TabControl.ItemsSource property: 通常,可以使用TabControl.ItemsSource属性看到它的定义如下:

<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. 为此,您需要创建一个具有HeaderTextBodyText属性的自定义类。 Then you'll need to create a public ObservableCollection<YourCustomClass> collection property in your code behind named YourCollectionProperty . 然后,您需要在名为YourCollectionProperty的代码中创建一个public ObservableCollection<YourCustomClass>集合属性。

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. 请注意,两个DataTemplate中的Binding会自动将其DataContext设置为YourCollectionProperty集合中的一个项目,这就是为什么与Prop属性的Binding不起作用的原因。

Try moving the foreach loop so that it fires when the _xamTabControl.Loaded event fires. 尝试移动foreach循环,以便在_xamTabControl.Loaded事件触发时触发。 That should do the trick 这应该够了吧

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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