【问题标题】:Not hitting my Data Template based on datatype in ContentControl, Why not?没有根据 ContentControl 中的数据类型点击我的数据模板,为什么不呢?
【发布时间】:2018-03-29 01:52:21
【问题描述】:

我的数据模板都没有根据 DataContext 的 DataType 显示。传递给 UserControl 的 DataContext 的实际对象是一个实体 (EntityFrameWork 6.0)。

我指定 DataType="{x:Type pf:Promotion}" 这是实体所基于的 POCO 类的名称。

(xmlns:pf="clr-namespace:PFModel;assembly=PFModel")

我迷路了,不知道问题出在哪里。感谢您提供任何帮助或提示。

<UserControl x:Class="PFPromoEditor.UserControls.CenterEditor"
         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" 
         xmlns:local="clr-namespace:PFPromoEditor.UserControls"
         xmlns:wpfTool="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit" 
         xmlns:pf="clr-namespace:PFModel;assembly=PFModel"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">     
<Grid>
    <Border BorderBrush="#FF000000" Margin="2" Padding="5" BorderThickness="1,1,1,1" CornerRadius="8,8,8,8">
        <ContentControl>
            <ContentControl.Resources>
                <DataTemplate DataType="{x:Type pf:Promotion}">
                    <TextBox Text="Promotion DATA type" />
                </DataTemplate>
                <DataTemplate DataType="{x:Type pf:Casino}">
                    <TextBox Text="Casino DATA type" />
                </DataTemplate>
                <DataTemplate DataType="{x:Type pf:Progressive}">
                    <TextBlock Text="Progressive DATA type" />
                </DataTemplate>
                <DataTemplate DataType="{x:Type pf:Detail}">
                    <TextBox Text="Detail DATA type" />
                </DataTemplate>
            </ContentControl.Resources>                
        </ContentControl>
    </Border>
</Grid>

<local:CenterEditor x:Name="CenterContent" DataContext="{Binding ElementName=promoMenu,Path=MySelectedItem }"/>

回答第一个问题,控件具有正确设置的数据上下文,其中包含 Promotion、Casino、Promotion 或 Detail 实体。

我也尝试过使用以下绑定属性:

  <DataTemplate DataType="{x:Type pf:Progressive}">
                    <Grid>
                        <TextBlock Text="Progressive DATA type" />
                        <TextBox Text="{Binding Path=Detail.Title, FallbackValue= 'Select any Item in list to edit'}"/>
                    </Grid>
                </DataTemplate>

什么都没有,空白。

我也刚刚放了类似的东西:

<TextBox Text="{Binding Path=Detail.Title, FallbackValue= 'Select any Item in list to edit'}"/>

在上面的代码中是这样的:

                </DataTemplate>
            </ContentControl.Resources>
            <TextBox Text="{Binding Path=Detail.Title, FallbackValue= 'Select any Item in list to edit'}"/>
        </ContentControl>

文本框绑定很好,实体在那里,我得到了预期的数据。

这不是数据绑定问题。我有上下文,我有一个合适的对象。

我想到了其他一些我需要尝试的事情。让我回复你。

【问题讨论】:

  • 您的 ContentControl 没有绑定? :-) ?你没有用数据填充它吗?所以它不会工作
  • 是的,它有数据上下文,当我填充像 这样的行时将其与来自适当对象的适当数据一起放在内容控件中。
  • 好的,那么没有设置 ContentControl 的“Content”属性。 DataContext 是不够的。
  • @Clemens 请解释一下。我知道实体类型被称为其他东西,例如 Progressive_B788B3F33208E4E024375DA9041620F7051E96C2EB606600E176440BCA2FCA86 但是您如何在数据模板类型中引用它?特别是因为实体类型中的数字发生了变化。
  • @GCamel 控件的子控件继承父控件的数据上下文,您在控件树中的某个点设置数据上下文的唯一原因是,如果您需要它不是父数据上下文。还是我错过了什么?

标签: wpf xaml data-binding entity-framework-6


【解决方案1】:

事实证明,我做错的事情(除了固执地认为我认为错了,并试图在凌晨 3 点解决我的问题,以及对问题的总体思考过度)被遗漏了:

Content="{Binding}"

顺便说一句,我在 cmets 中错过了哪种方式,我记得阅读 datacontext,这是因为我感到疲倦和沮丧。

解决问题的是对内容控件的修改

 <ContentControl  Content="{Binding}" >

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    • 2016-05-31
    • 2022-11-10
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多