在DataTemplate DataType中使用命名空间

3
我正在尝试使用ListBox和ItemsSource属性从YouTube显示视频列表。我现在拥有的代码可以工作(如下),但现在我需要格式化我的数据。
<ListBox Name="lbVideos" ItemsSource="{Binding Source={StaticResource listOfVideos}}"/>

为此我使用了DataTemplate,但问题在于类型是Google.YouTube.Video。

<Application x:Class="YouTube_Notifier.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Startup="AppStartup"
    xmlns:src="clr-namespace:YouTube_Notifier" 
    xmlns:System="clr-namespace:System;assembly=mscorlib">
    <Application.Resources>
        <DataTemplate DataType="{x:Type src:Google:YouTube:Video}">
        </DataTemplate>
    </Application.Resources>
</Application>

上面的代码导致我收到错误信息:"Type 'src:Google.YouTube.Video' was not found."

我的问题是如何在DataTemplate中使用命名空间?

1个回答

9

包含您的类型的命名空间需要在您的xmlns属性中映射,即

xmlns:src="clr-namespace:YouTube_Notifier.Google.YouTube" 

{x:Type src:Video}

请参阅命名空间映射参考x:Type参考文档...


这比我想象的要容易。谢谢! :-) - Dumpen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接