简体   繁体   English

如何在WPF中使用TabItem和DataTemplate绑定?

[英]How to use binding in a TabItem with DataTemplate in WPF?

I'm trying to do something basic (I think!), but I've got a problem. 我正在尝试做一些基本的事情(我想!),但是我遇到了一个问题。 I have got a TabControl and 3 TabItems. 我有一个TabControl和3个TabItems。 The 2 first items must be untouched, and I want to apply a dataTemplate on the third tabItem, so I used a DataTemplateSelector. 前两个项目必须保持不变,并且我想在第三个tabItem上应用dataTemplate,因此我使用了DataTemplateSelector。 This is OK, it works. 可以,可以。 But then, I want to fill data in the third tabItem with my datamodel. 但是然后,我想用我的数据模型填充第三个tabItem中的数据。 Binding is not working because my DataContext is always "null" in the tabItem. 绑定不起作用,因为我的DataContext在tabItem中始终为“ null”。 How can I set the DataContext in the tabItem created by the dataTemplate ? 如何在dataTemplate创建的tabItem中设置DataContext? Here is my code : 这是我的代码:

XAML : XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="910" Width="1200">

<Window.Resources>
    <DataTemplate x:Key="configurationTemplate" DataType="{x:Type local:ConfigurationDatamodel}">
        <local:ConfigurationTemplateUC DataContext="{Binding DataContext.ConfigurationDatamodel}" />
    </DataTemplate>

    <local:TabItemTemplateSelector ConfigurationTemplate="{StaticResource configurationTemplate}" x:Key="tabItemTemplateSelector"/>
</Window.Resources>

<Grid>
    <TabControl Height="800" HorizontalAlignment="Stretch" Margin="0,0,0,0" Name="tabControl1" VerticalAlignment="Top" ContentTemplateSelector="{StaticResource tabItemTemplateSelector}">
        <TabItem Header="TabItem1" Name="tabItem1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
            <some stuff />
        </TabItem>
        <TabItem Header="TabItem2" Name="tabItem2" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
            <some stuff />
        </TabItem>
        <TabItem Header="TabItem3" Name="tabItem3" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">

        </TabItem>
    </TabControl>
</Grid>
</Window>

UserControl of my DataTemplate : 我的DataTemplate的UserControl:

<UserControl x:Class="WpfApplication1.ConfigurationTemplateUC"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="800" d:DesignWidth="1000">
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
    <Label Content="File name : " Height="28" HorizontalAlignment="Left" Margin="50,43,0,0" Name="label1" VerticalAlignment="Top" FontSize="16"/>
    <Label Content="{Binding Path=File}" FontSize="16" Height="28" HorizontalAlignment="Left" Margin="160,43,0,0" Name="label2" VerticalAlignment="Top" Width="324" />
</Grid>
</UserControl>

Datamodel : 数据模型:

public class ConfigurationDatamodel
{
    private string file;

    public string File
    {
        get { return this.file; }
        set 
        { 
            this.file= value;
        }
    }

    public ConfigurationDatamodel()
    {}

    public ConfigurationDatamodel(string file)
    {
        this.file= file;
    }
}

Code-behind : 后台代码:

public MainWindow()
    {
        ConfigurationDatamodel dt1 = new ConfigurationDatamodel("example.txt");
        InitializeComponent();

        tabItem3.DataContext = dt1;
    }

There is no binding errors in the console, but the label containing the filename is always empty. 控制台中没有绑定错误,但是包含文件名的标签始终为空。 The DataContext of the UserControl "ConfigurationTemplateUC" is always "null". UserControl“ ConfigurationTemplateUC”的DataContext始终为“ null”。

Any thoughts? 有什么想法吗?

EDIT 编辑

If I set the DataContext in the contructor of the UserControl, it works : 如果我在UserControl的构造函数中设置DataContext,它将起作用:

public ConfigurationTemplateUC()
    {
        InitializeComponent();
        ConfigurationDatamodel dt1 = new ConfigurationDatamodel("example.txt");
        this.DataContext = dt1;
    }

How can I set this dataContext with the DataTemplate ? 如何使用DataTemplate设置此dataContext?

In your DataTemplate , Remove the DataContext binding from 在您的DataTemplate ,从以下位置删除DataContext绑定:

<local:ConfigurationTemplateUC DataContext="{Binding DataContext.ConfigurationDatamodel}" />

...leaving only ...只剩下

<local:ConfigurationTemplateUC />

UI in a DataTemplate automatically gets the bound data as its DataContext , so in your case you override the correct DataTemplate with a path to something that can't be found. DataTemplate UI会自动将绑定的数据作为其DataContext ,因此在您的情况下,将正确的DataTemplate替换为无法找到的内容的路径。

EDIT: 编辑:

Also, you don't need a DataTemplateSelector to do this. 另外,您不需要DataTemplateSelector即可执行此操作。 You can remove the selector and instead simply use TabItem.ContentTemplate : 您可以删除选择器,而只需使用TabItem.ContentTemplate

<TabItem Header="TabItem3" Name="tabItem3" ContentTemplate="{StaticResource configurationTemplate}" >
    ...

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

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