【发布时间】:2018-03-10 23:14:42
【问题描述】:
在我的名为 Type 的 POCO 类上绑定我的自定义属性时,我无法将 <ContentControl Content={Binding Type}/> 绑定到本地资源 <DataTemplate>(我希望这不是因为命名的错误选择)。
ReportItemModel
public class ReportItemModel
{
public ReportItemModel(string name, ReportItemType itemType, Type type)
{
Name = name;
ItemType = itemType;
Type = type;
}
public string Name { get; }
public ReportItemType ItemType { get; }
public Type Type { get; }
}
XAML
<ContentControl Content="{Binding Type}" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type moduleTypes:ReportModule}">
<Label>Test</Label>
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
Type 属性是(您可能已经猜到了)System.Type,而我的 WPF 应用程序中的输出始终是 ToString() 的输出,我猜这是因为我的 @ 匹配失败987654329@。如果我将ContentControl 更改为Content={Binding} 并将<DataTemplate> 设置为接受POCO 类ReportItemModel 的数据类型,它将按预期工作。我能看到的唯一区别是 ReportItemModel 已经实例化,而 Type 属性没有。
【问题讨论】: