实际上我想在每个pivot项目的头部设置图像.....在运行时使用资源
<UserControl x:Class="WindowsPhoneApplication7.heder"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="18" d:DesignWidth="24" Loaded="UserControl_Loaded">
<UserControl.Resources>
<DataTemplate x:Key="heder_image">
<Image Source="/WindowsPhoneApplication7;component/Images/appbar.feature.settings.rest.png"></Image>
</DataTemplate>
</UserControl.Resources>
它是一种资源现在我的pivot项目在另一个cs文件
heder dis =new heder();
pivotItem[i] = new PivotItem();
pivotItem[i].Header = (DataTemplate)dis.Resources["heder_image"];
但是头模板没有设置......图片不显示
您在这里做错的是,您将PivotItem
的Header
设置为模板,而不是使用Pivot.HeaderTemplate。这样就不需要在c#中生成项目,而是通过数据绑定(通过。主元。ItemsSource属性),并按照您的意思在XAML中完成所有的样式。
话虽如此,我们还是不鼓励在Pivot Header中使用图像,因为它违背了平台的默认外观,因此也违背了平台的UI和UX指南。
检查图像的Build Action
是否设置为Content
(参见VS2010资源管理器中的文件属性)。如果图像与XAML页面在同一个项目中,请使用"/Images/appbar.feature.settings.rest.png"
而不是"/WindowsPhoneApplication7;component..."
。
使用这个XAML片段并在XAML:
中设置标题<controls:Pivot ItemsSource="{Binding MyObjects}" HeaderTemplate="{StaticResource heder_image}">
<controls:Pivot.ItemTemplate>
<DataTemplate>
...
</DataTemplate>
</controls:Pivot.ItemTemplate>
</controls:Pivot>