【问题标题】:Assigning datacontext to autogenerated tabitem in tabcontrol将 datacontext 分配给 tabcontrol 中的自动生成的 tabitem
【发布时间】:2014-09-25 08:36:17
【问题描述】:

我正在尝试将对象分配给 TabItem 中的数据上下文。要了解一下,请查看以下代码示例

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

如您所见,我使用数据模板作为 TabItem 内容外观,TextBox。 TextBox Text 属性绑定到数据上下文中的属性。

还有来自 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() });
        }
    }
}

作为 TabItem 内容的输出,我什么都没有,所以内容是空的,为什么?

【问题讨论】:

    标签: c# wpf


    【解决方案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&lt;YourCustomClass&gt; 集合属性。

    请注意,两个DataTemplates 中的Bindings 将自动将其DataContexts 设置为YourCollectionProperty 集合中的一个项目,这就是为什么您的Binding 设置为Prop 属性没用。

    【讨论】:

    • 我正在使用基础设施控制。我的目标是动态创建 TabItem,这取决于有多少项目可用。例如,如果有 5 个项目,则创建 5 个选项卡。那是因为上面我做了一个 foreach 来创建新标签,这取决于 Items。
    • 你仍然可以这样做。对于每个必需的 TabItem,只需将另一个项目添加到您的 YourCollectionProperty 集合中。在 WPF 中使用ItemsSource 属性优于使用Items 属性。
    • 我尝试关注但不起作用gist.github.com/kostonstyle/b54b453ae664cf12d222
    • 您的Collection 字段应该是一个属性。
    【解决方案2】:

    尝试移动 foreach 循环,使其在 _xamTabControl.Loaded 事件触发时触发。这应该可以解决问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-14
      • 2021-12-28
      • 2011-01-28
      • 1970-01-01
      • 2012-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多