繁体   English   中英

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

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

我正在尝试做一些基本的事情(我想!),但是我遇到了一个问题。 我有一个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>

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

数据模型:

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?

在您的DataTemplate ,从以下位置删除DataContext绑定:

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

...只剩下

<local:ConfigurationTemplateUC />

DataTemplate UI会自动将绑定的数据作为其DataContext ,因此在您的情况下,将正确的DataTemplate替换为无法找到的内容的路径。

编辑:

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

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

暂无
暂无

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

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