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

本文关键字:DataTemplate 绑定 TabItem WPF | 更新日期: 2023-09-27 17:49:35

我想做一些基本的事情,但是我有一个问题。我有一个TabControl和3 TabItems。前两个项目必须保持不变,我想在第三个tabItem上应用一个dataTemplate,所以我使用了一个DataTemplateSelector。这是好的,它工作。然后,我想用我的数据模型填充第三个tabItem中的数据。绑定不工作,因为我的DataContext在tabItem中总是"null"。如何在dataTemplate创建的tabItem中设置DataContext ?下面是我的代码:

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:

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

后台代码:

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

在控制台中没有绑定错误,但是包含文件名的标签总是空的。UserControl "ConfigurationTemplateUC"的DataContext始终为"null".

任何想法吗?

编辑

如果我在UserControl的构造函数中设置DataContext,它会起作用:

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

如何使用DataTemplate来设置这个dataContext ?

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

在您的DataTemplate中,从

删除DataContext绑定
<local:ConfigurationTemplateUC DataContext="{Binding DataContext.ConfigurationDatamodel}" />

…只留下

<local:ConfigurationTemplateUC />

UI在一个DataTemplate自动获得绑定的数据作为它的DataContext,所以在你的情况下,你覆盖正确的DataTemplate的路径无法找到的东西。

编辑:

同样,您不需要DataTemplateSelector来完成此操作。您可以删除选择器,而只需使用TabItem.ContentTemplate:

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