我想从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内容,为什么呢?
我正在使用infragistics控件。我的目标是创建TabItem动态,它取决于有多少项目可用。例如,如果5个项目然后创建5个选项卡。那是因为上面我做了一个foreach来创建新标签,取决于Items。 – 2014-09-25 09:45:54
你仍然可以这样做。对于每个需要的'TabItem',只需将另一个项目添加到'YourCollectionProperty'集合中。使用'ItemsSource'属性比使用WPF中的'Items'属性更可取。 – Sheridan 2014-09-25 09:48:53
我试过以下但不起作用https://gist.github.com/kostonstyle/b54b453ae664cf12d222 – 2014-09-26 05:47:04