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

时间:2014-03-11 14:13:14

标签: c# wpf binding datatemplate tabcontrol

我试图做一些基本的事情(我想!),但我遇到了问题。我有一个TabControl和3个TabItems。 2个第一项必须不受影响,我想在第三个tabItem上应用dataTemplate,所以我使用了DataTemplateSelector。这没关系,它有效。但是,我想用我的datamodel填充第三个tabItem中的数据。绑定不起作用,因为我的DataContext总是" null"在tabItem中。如何在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>

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

代码隐藏:

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

        tabItem3.DataContext = dt1;
    }

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

有什么想法吗?

修改

如果我在UserControl的构造函数中设置DataContext,它可以工作:

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

如何使用DataTemplate设置此dataContext?

1 个答案:

答案 0 :(得分:0)

DataTemplate中,从

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

......只留下

<local:ConfigurationTemplateUC />

DataTemplate中的用户界面会自动将绑定数据作为其DataContext获取,因此在您的情况下,您会覆盖正确的DataTemplate,并找到无法找到的内容的路径。

修改

另外,您不需要DataTemplateSelector来执行此操作。您可以删除选择器,而只需使用TabItem.ContentTemplate

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