2014-09-25 37 views
0

我想从TabItem中分配一个对象到datacontext。为了得到一个想法,看看下面的代码示例将数据上下文分配给tabcontrol中的自动生成的tabite

<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> 

正如你所看到的,我使用DataTemplate中进行的TabItem内容的外观,文本框。 TextBox Text属性绑定到datacontext中的属性。

而且从用户控件

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() }); 
     } 
    } 
} 

部分类作为TabItem的内容输出我没有什么,所以emtpy内容,为什么呢?

回答

1

您似乎没有正确声明您的TabControl XAML。这是习惯看到它定义了更多像这样,使用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> 

对于这个工作,你需要创建中有HeaderTextBodyText属性的自定义类。然后,您需要在名为YourCollectionProperty的代码中创建一个public ObservableCollection<YourCustomClass>集合属性。

请注意,Binding S中的2分DataTemplate的内线将自动拥有自己的DataContext S设定从YourCollectionProperty收集的项目,这就是为什么你BindingProp财产没有工作。

+0

我正在使用infragistics控件。我的目标是创建TabItem动态,它取决于有多少项目可用。例如,如果5个项目然后创建5个选项卡。那是因为上面我做了一个foreach来创建新标签,取决于Items。 – 2014-09-25 09:45:54

+0

你仍然可以这样做。对于每个需要的'TabItem',只需将另一个项目添加到'YourCollectionProperty'集合中。使用'ItemsSource'属性比使用WPF中的'Items'属性更可取。 – Sheridan 2014-09-25 09:48:53

+0

我试过以下但不起作用https://gist.github.com/kostonstyle/b54b453ae664cf12d222 – 2014-09-26 05:47:04

0

尝试移动foreach循环,以便在_xamTabControl.Loaded事件触发时触发。这应该会诀窍

相关问题