C# ContentControl无法绑定到DataTemplate
在名为C# ContentControl无法绑定到DataTemplate,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,在名为Type的POCO类上绑定自定义属性时,我无法将绑定到本地资源(我希望这不是因为命名选择不当) ReportItemModel public class ReportItemModel { public ReportItemModel(string name, ReportItemType itemType, Type type) { Name = name; ItemType = itemType;
Type
的POCO类上绑定自定义属性时,我无法将
绑定到本地资源
(我希望这不是因为命名选择不当)
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
属性是(正如您可能猜到的)一个系统。Type
并且我的WPF应用程序中的输出总是ToString()
的输出,我猜这是因为我的
匹配失败。如果我将ContentControl
更改为Content={Binding}
,并将
设置为接受POCO类的数据类型ReportItemModel
,它将按预期工作。我能看到的唯一区别是,ReportItemModel
已经实例化,而类型
属性没有实例化 问题在于XAMLDataType=“{x:Type moduleTypes:ReportModule}”
在System.Type
上幕后调用GetType()
,它总是返回一个,因为对象从未实例化过,而且总是(此时)一个System.Type
(至少可以说是一个“大脑放屁”的时刻)。通过在绑定