如何绑定到ContentControl的数据表中的数据 [英] How to Bind To Data within a Datatemplate of a ContentControl

查看:727
本文介绍了如何绑定到ContentControl的数据表中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下简化示例:

 < Window x:Class =TemplateBinding.MainWindow
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
Title =MainWindowHeight =350Width =525>
< Window.Resources>
< ResourceDictionary>
< ResourceDictionary.MergedDictionaries>
< ResourceDictionary
Source =pack:// application:,,, / TemplateBinding; component / PersonTemplate.xaml/>
< /ResourceDictionary.MergedDictionaries>
< / ResourceDictionary>
< /Window.Resources>
< Grid>
< ContentControl ContentTemplate ={StaticResource PersonTemplate}/>
< / Grid>
< / Window>

With:

 code>< ResourceDictionary xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/ 2006 / xaml>

< DataTemplate x:Key =PersonTemplate>
< Border Width =100Height =100Background =RosyBrown>
< TextBlock Text ={Binding Path = FirstName}VerticalAlignment =CenterTextAlignment =Center/>
< / Border>
< / DataTemplate>

< / ResourceDictionary>

作为我单独的ResourceDictionary文件中的DataTemplate。



我将DataContext设置在MainWindow的Construcor中,并通过显示如下这样的名字来验证:< ContentControl Grid.Row =1Content ={Binding FirstName} / / code>。



在另一个场景中,我使用一个带有 ListBox的数据模板我在我的DataTemplate中完全相同的方式,它只是工作。



我知道DataTemplate正在工作,除了绑定,因为它正确显示大小和背景颜色。



我做错了什么?我的DataTemplate中的绑定怎么样?

解决方案

你需要绑定 code> - ContentControl的属性

 < ContentControl Content ={Binding}ContentTemplate ={StaticResource PersonTemplate }/> 

这将将ContentControl的DataContext设置为控件的内容。



只设置 ContentTemplate 属性是不够的。 ContentControl不会将其DataContext隐含地用作内容。


I have the following simplified Example:

    <Window x:Class="TemplateBinding.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary
                            Source="pack://application:,,,/TemplateBinding;component/PersonTemplate.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Window.Resources>
        <Grid>
            <ContentControl ContentTemplate="{StaticResource PersonTemplate}" />
        </Grid>
    </Window>

With:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

        <DataTemplate x:Key="PersonTemplate">
            <Border Width="100" Height="100" Background="RosyBrown">
                <TextBlock Text="{Binding Path=FirstName}" VerticalAlignment="Center" TextAlignment="Center"/>
            </Border>
        </DataTemplate>

    </ResourceDictionary>

as my DataTemplate in a separate ResourceDictionary file.

I set my DataContext in the Construcor of my MainWindow and have verified it by just displaying the first name like this: <ContentControl Grid.Row="1" Content="{Binding FirstName}"/>.

In an other scenario wher I use a DataTemplate with a ListBox I do the Binding exactly the same way in my DataTemplate and it just works.

I know that the DataTemplate is working except the binding because it correctly shows the size and background color.

What am I doing wrong? How would the Binding in my DataTemplate have to look?

解决方案

You need to bind the Content-Property of the ContentControl

<ContentControl Content="{Binding}" ContentTemplate="{StaticResource PersonTemplate}" />

This will set the DataContext of the ContentControl as Content of the control.

Setting only the ContentTemplate property is not enough. The ContentControl does not implicitly use its DataContext as Content.

这篇关于如何绑定到ContentControl的数据表中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆