Datatemplate绑定应该有效,但不能。有这个外部来源吗?

时间:2011-11-29 17:17:15

标签: wpf binding datatemplate

我有一个内容控件,它不会仅显示静态文本的绑定数据:

<ContentControl Name="Detail" Grid.Row="3" Grid.ColumnSpan="3"
            ContentTemplate="{StaticResource detailsAdListingTemplate}" 
            Margin="9,0,0,0"/>

<DataTemplate x:Key="detailsAdListingTemplate" >
            <Grid Margin="5,5,5,10">
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="113"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Row="0" Grid.Column="0" Margin="0,0,8,0"
                           Name="title"
                           Style="{StaticResource smallTitleStyle}">Title:</TextBlock>
                <TextBlock Name="DescriptionDTKey" Grid.Row="0" Grid.Column="1" 
                    Text="{Binding Path=Title}" 
                    Style="{StaticResource textStyleTextBlock}"/>

                <TextBlock Grid.Row="1" Grid.Column="0" Margin="0,0,8,0"
                           Name="price"
                           Style="{StaticResource smallTitleStyle}">Price:</TextBlock>
                <StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal">
                    <TextBlock Text="$" Style="{StaticResource textStyleTextBlock}"/>
                    <TextBlock Name="PriceDTKey" 
                        Text="{Binding Path=Price}" 
                        Style="{StaticResource textStyleTextBlock}"/>
                </StackPanel>

                <TextBlock Grid.Row="2" Grid.Column="0" Margin="0,0,8,0"
                         Name="date"
                         Style="{StaticResource smallTitleStyle}">Date:</TextBlock>
                <TextBlock Name="DateDTKey" Grid.Row="2" Grid.Column="1" 
                  Text="{Binding Path=Date}" 
                  Style="{StaticResource textStyleTextBlock}"/>
                <TextBlock Grid.Row="3" Grid.Column="0" Margin="0,0,8,0"
                            Name="subCategory"
                            Style="{StaticResource smallTitleStyle}">Subcategory:   </TextBlock>
                <TextBlock Name="SubCategoryDTKey" Grid.Row="3" Grid.Column="1" 
                    Text="{Binding Path=SubCategory.Name}" 
                    Style="{StaticResource textStyleTextBlock}"/>

                <TextBlock Grid.Row="4" Grid.Column="0" Margin="0,0,8,0"
                            Name="owner"
                           Style="{StaticResource smallTitleStyle}">Owner:</TextBlock>
                <TextBlock Name="OwnerDTKey" Grid.Row="4" Grid.Column="1" 
                    Text="{Binding Path=User.Username}" 
                    Style="{StaticResource textStyleTextBlock}"/>

                <TextBlock Grid.Row="5" Grid.Column="0" Margin="0,0,8,0"
                           Name="location"
                           Style="{StaticResource smallTitleStyle}">Location:  </TextBlock>
                <TextBlock Name="locationDTKey" Grid.Row="5" Grid.Column="1" 
                    Text="{Binding Path=Location}"
                    Style="{StaticResource textStyleTextBlock}"/>

                <TextBlock Grid.Row="6" Grid.Column="0" Margin="0,0,8,0"
                           Name="Body"
                           Style="{StaticResource smallTitleStyle}">Body:</TextBlock>
                <TextBlock Name="BodyDTKey" Grid.Row="6" Grid.Column="1" 
                    Text="{Binding Path=Body}" 
                    Style="{StaticResource textStyleTextBlock}"/>

            </Grid>
        </DataTemplate>

我设置了Detail.DataContext = ad; (其中ad是一个合适的Ad对象,其中包含{Binding Path = Property}中的所有数据和所有属性。

用于精细工作。我不记得编辑了。我不知道我对应用程序做了什么,但现在是数据模板 仅显示静态文本 - 例如标题:但没有实际标题。因此只有绑定不起作用。

我确定上面的xaml代码没有任何问题,它应该有效。

是否存在可能导致此问题的“外部”其他来源?

1 个答案:

答案 0 :(得分:3)

ContentTemplate是用于显示ContentControl.Content的模板,您将其留作null

设置Detail.Content = ad;(而不是Detail.DataContext),或将ContentControl.Content属性绑定到当前的DataContext。

<ContentControl Name="Detail" Grid.Row="3" Grid.ColumnSpan="3" Margin="9,0,0,0"
                Content="{Binding }"
                ContentTemplate="{StaticResource detailsAdListingTemplate}" />